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

在angularjs测试中验证对$httpBackend的调用,angularjs,unit-testing,jasmine,Angularjs,Unit Testing,Jasmine,我正在尝试为angularjs编写一个测试,它模拟$httpBackend。下面是测试 describe('test', function() { beforeEach(function (){ module('abcApp'); }); var $httpBackend, filterService; beforeEach(inject(function($injector) { $httpBackend = $injecto

我正在尝试为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();
    });
});
运行测试时,出现以下错误:- 错误:应为间谍,但未定义

我该如何断言$httpBackend.get已使用expect使用必需参数调用

expect($httpBackend.get).toHaveBeenCalled();
这是无效的。首先是因为$httpBackend没有任何(文档化的)get()方法。其次,因为$httpBackend.get没有被Jasmine发现,所以如果调用了这个方法,就不能添加Jasmine

您应该测试一下,如果http后端返回空数组,服务将返回正确的内容,或者具有所需的副作用。你也可以使用

$httpBackend.expectGET('api/Abc').respond([]);

因此,$httpBackend将为您验证预期的请求是否已发送

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