Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/ant/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Angularjs 如何模拟$http来给出一个404?_Angularjs_Mocking_Jasmine - Fatal编程技术网

Angularjs 如何模拟$http来给出一个404?

Angularjs 如何模拟$http来给出一个404?,angularjs,mocking,jasmine,Angularjs,Mocking,Jasmine,如何模拟$http,以便在调用特定URL时调用.error函数 beforeEach(angular.mock.inject(function ($httpBackend) { $httpBackend.when('GET', '/ErrorReturningURL').respond(/* report a 404 */); })); 在我的单元测试中,我将观察某个函数调用。您可以在返回的值上使用 beforeEach(angular.mock.inject(function ($h

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

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

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

您可以在
返回的值上使用

beforeEach(angular.mock.inject(function ($httpBackend) {
    $httpBackend.when('GET', '/ErrorReturningURL').respond(404, '');
}));
我正在使用语法
函数([status,]data[,headers])
来响应
函数。因此,我需要显式地传递第二个参数
data
,以确保第一个参数被解释为
status