Angularjs-mock testing $http by mocking using httpBackend and flush() not working.

Background

I was writing a unit test for my angularjs app and I ran into the following problem.

I was setting up the test correctly to inject in the $httpBackend to mock out the http calls, but the when I called called the service and then called $httpBackend.flush() it would return an error:

httpBackend no pending request to flush

Which is not what I was expecting.

Other background

When I commented out the $httpBackend.whenGET
The following 2 methods in the afterEach function would not throw an error either.

$httpBackend.verifyNoOutstandingExpectation();
$httpBackend.verifyNoOutstandingRequest();

This would pass the test even though the expectations were never triggered.

Here is an example of the code I was testing with.

Continue reading

Phantomjs and testem

I ran into this problem with Testem not picking up phantomjs from the command line to run the tests.

My testem.json

{
“launch_in_dev”: [
"phantomjs"
]

}

The problem

npm folder where the installs occur for globals was still owned by root.

If I tried to run phantomjs from the command line it would throw up a stack trace about ownership.

The solution Chown

sudo chown -R the location where the global node modules are installed by npm.

OSX

sudo chown -R YOURUSER:YOURGROUP /usr/local/lib/node_modules/

Linux

sudo chown -R YOURUSER:YOURGROUP /usr/lib/node_modules/

Phantomjs cannot be started and local install

Do not put phantomjs in your package.json of your node project.

When you call npm install it will install the package locally in your project’s node_modules folder.

When you try and run grunt test you will get an error message similar to this.

phantomjs cannot be started.

Fixing local phantomjs install

rm node_modules

npm install

Phantomjs must be installed globally.

npm install -g phantomjs

Angularjs and Grails withFormat and json

I have been doing a mixed angularjs and grails project and ran into problems getting json to come back from the controller request with a withFormat block.

What I expected

I expected that it would call the grails app controller index method and with the withFormat to pick out that the request came is as a json request so return json. This is not the case, it would send me back the html page instead.

The problem was that grails would see */* as the accept headers and return just the html.

Grails code from the grails controller

def index() {
        def books = Book.findAll().collectEntries { book -> [book.title, book] }.values()
        withFormat {
            html books: books
            json { render books as JSON }
        }
    }

Angularjs json request that returns html

$http.get('/book/index').success(function(response) { };
}

Continue reading

Yeoman migration to 1.0 and grunt with coffeescript

With migrating to yeoman 1.0 from version 0.9.6 you have to essentially create a new project and import your source over as the builds have changed so much.  Check the documentation from yeoman on how to do the upgrade.

https://github.com/yeoman/yeoman/wiki/Migrate-from-0.9.6-to-1.0

I have an Angularjs project with coffeescript that I wanted to have compile to individual js files. There are lots of good examples on how grunt works with coffeescript from the source https://github.com/roytruelove/angular-grunt-coffeescript

Purpose of compiling coffeescript this way.

This will create 1 js file per coffee file and have them sit next to the original source coffeescript file.  This is not what you want to release to production as there are a lot of network requests per page.

Continue reading