gpt4 book ai didi

angularjs - 在 angularjs 测试中验证对 $httpBackend 的调用

转载 作者:行者123 更新时间:2023-12-01 11:32:45 24 4
gpt4 key购买 nike

我正在尝试为 angularjs 编写一个模拟 $httpBackend 的测试。以下是测试。

describe('test', function() {

beforeEach(function (){
module('abcApp');
});

var $httpBackend, filterService;
beforeEach(inject(function($injector) {
$httpBackend = $injector.get('$httpBackend');
filterService = $injector.get('filterService');
}));

it('getCompany calls get on http with correct parameter', function () {
$httpBackend.when('GET', 'api/Abc').respond([]);
filterService.getAbc();
$httpBackend.flush();
expect($httpBackend.get).toHaveBeenCalled();
});
});

当我运行测试时,我收到以下错误:-
错误:期待 spy ,但未定义。

任何想法我如何断言 $httpBackend.get 已经使用期望调用了必需的参数。

最佳答案

expect($httpBackend.get).toHaveBeenCalled();

是无效的。首先是因为 $httpBackend 没有任何(记录在案的)get() 方法。其次,因为$httpBackend.get 没有被Jasmine 监听,所以如果调用了这个方法就不能添加Jasmine。

您应该测试一下,鉴于 http 后端返回的空数组,该服务返回正确的内容,或具有所需的副作用。你也可以使用
$httpBackend.expectGET('api/Abc').respond([]);


$httpBackend.verifyNoOutstandingExpectation();
$httpBackend.verifyNoOutstandingRequest();

以便 $httpBackend 为您验证预期的请求已发送。

关于angularjs - 在 angularjs 测试中验证对 $httpBackend 的调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30829379/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com