How do you mock $http to give a 404?
angularjs, jasmine, mocking
Solution
You can use the alternate form of the `.respond` function on the value returned by `when`:
beforeEach(angular.mock.inject(function ($httpBackend) {
$httpBackend.when('GET', '/ErrorReturningURL').respond(404, '');
}));
I am using the syntax `function([status,] data[, headers])` for the `respond` function. Hence, I need to explicitly pass in the second argument `data` to ensure that the first argument is interpreted as `status`.
Problem
How do you mock `$http` so that when you call a specific URL the `.error` function is called? ``` beforeEach(angular.mock.inject(function ($httpBackend) { $httpBackend.when('GET', '/ErrorReturningURL').respond(/* report a 404 */); })); ``` In my unit test I will be watching for a certain function call.