你如何模拟$ http给404?

Fut*_*oad 5 mocking jasmine angularjs

你如何模拟,$http以便在调用特定的URL时调用该.error函数?

beforeEach(angular.mock.inject(function ($httpBackend) {
    $httpBackend.when('GET', '/ErrorReturningURL').respond(/* report a 404 */);
}));
Run Code Online (Sandbox Code Playgroud)

在我的单元测试中,我将观察某个函数调用.

mus*_*_ut 8

您可以在返回的值上使用.respond函数的替代形式when:

beforeEach(angular.mock.inject(function ($httpBackend) {
    $httpBackend.when('GET', '/ErrorReturningURL').respond(404, '');
}));
Run Code Online (Sandbox Code Playgroud)

我正在使用function([status,] data[, headers])respond函数的语法.因此,我需要显式传递第二个参数data以确保第一个参数被解释为status.