How to Mock JSON with $httpBackend in Jasmine

PUBLISHED ON AUG 8, 2014 — IMPORTED FROM TUMBLR, TEXT

There is a lot of information out there on running tests for Angular using Jasmine + Karma. One thing that was tripping me up, though, was being able to access a mock JSON file, rather than trying to hit a server with an HTTP request every test.

Working from this code sample, I injected the mock file with the beforeEach call. However, I wasn’t able to access the JSON response until I injected the defaultJSON directly into the specific test:

it('should get a Success response from Auth', inject( function (defaultJSON) {
   expect(defaultJSON.status).toBe('Success');
   expect(defaultJSON.object.id).toBe(5);
   httpBackend.flush();
}));

All green.

[ more testing resources ]