Tag Archives: $http

Unit testing angularjs with $http

Angularjs unit testing with $http.

How it all hangs together

When calling external servers to get or push data you can use the $http from angular.  $http has a set of methods like Get, Post and Put like you would expect.

$http callback methods.

It treats the call much like a ajax call where it has a .success method and a .error method which it returns to depending on if the call success or failure.

Scoping of parameters and .success() and .errror()

You can interact with the $scope or $rootScope to set or change data.  The scoping of the .success() and .error() means you cannot use a method level parameter and expect the value to be assigned before the the method ends.  It can go out of scope before it is guaranteed to be set.

Continue reading

Advertisements

angularjs e2e test with $http and out of memory exception

My experience with $httpBackend with e2e tests and out of memory exceptions.

$httpBackend and out of memory exceptions

I am running yadda with casperjs to do functional end to end testing.

I was doing a simple $http request that would expect some JSON back.  I was getting through 2.5gb of ram before my Casperjs test would fall over with out of memory exception and Phantomjs telling me how to report this bug.  I followed the instructions to get an error report for Phantomjs and it was not giving me any useful information for a bug report, so it was something in my code that was the problem.

What I figured out:

Returning invalid JSON and making lots calls to the $httpBackend in quick succession then I experienced my tests falling over very quickly with out of memory exceptions.  It also slows down the tests as the memory does not get released very quickly.

If you make lots of calls to $httpBackend full stop then it tends to chew through lots of memory.  Use it sparingly and have your code against calling it too frequently as it is very expensive.

There is caching available for on $http requests but I have not used it yet. $http