A Couple of Essentials for Your AngularJS Unit Testing Toolkit

Here's a couple of AngularJS features to help with unit testing controllers that I was able to take advantage of this past weekend.

Loading json test data from a file:

You can use the jasmine-jquery library for a simple api for loading json from a file (among other useful features.  For a controller, you can use as follows...

//you have to use "base" to address the root of your project  
jasmine.getJSONFixtures().fixturesPath= 'base/data';  
peopleData = getJSONFixture('people.json');  

Simulating http response data with $httpBackend:

Once you have json data loaded, you will want to supply it to the $http process so that you can control the data that comes back.  You can do this through through the $httpBackend object

$httpBackend = $injector.get('$httpBackend');   
$httpBackend.whenGET('/api/people').respond(200, peopleData);  

or to simulate an error:

$httpBackend.whenGET('/api/people').respond(500, '');  
and since the $http calls are asynchronous, you can simulate the server responding by calling:

You can use these json file load and $httpBackend together to create a nice, easy way to stub json response data.

Further info for your reading enjoyment...

$httpBackend docs jasmine-jquery