Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you mock $http to give a 404?

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.

like image 766
FutuToad Avatar asked Jan 27 '14 10:01

FutuToad


1 Answers

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.

like image 186
musically_ut Avatar answered Nov 15 '22 05:11

musically_ut