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