在我们的内部angularjs项目中,有一个服务调用了
为了测试,我使用
运行测试结果出现以下错误:
$http.head()
,我正在尝试对其进行测试。为了测试,我使用
angular-mocks
提供的Fake HTTP backend。以下是相关代码:it('handle status code 200', inject(function ($httpBackend, ConnectionService) {
spyOn(Math, 'random').andReturn(0.1234);
httpBackend = $httpBackend;
httpBackend.expectHEAD('ping?rand=1234').respond(200);
ConnectionService.sendRequest();
httpBackend.flush();
expect(ConnectionService.stats.packetsReceived).toEqual(5);
httpBackend.verifyNoOutstandingExpectation();
httpBackend.verifyNoOutstandingRequest();
}));
运行测试结果出现以下错误:
PhantomJS 1.9.7 (Mac OS X) connection service tests sendRequest function handle status code 200
FAILED TypeError: 'undefined' is not a function (evaluating 'httpBackend.expectHEAD('ping?rand=1234')')
at /path/to/app/app-connection-service_test.js:66
at d (/path/to/app/bower_components/angular/angular.min.js:35)
at workFn (/path/to/app/bower_components/angular-mocks/angular-mocks.js:2159)
经过一番搜索,我找到了相关的Github问题:
据我所知,这意味着在angular-mocks中真的没有expectHEAD()
方法 - 它已经被记录下来,但实际上它还不是稳定版angular的一部分。
最好的解决方法是什么?
请注意,我必须使用angular <= 1.2,因为此应用程序需要在IE8上运行(Angular 1.3 停止支持IE8)。
目前我正在考虑的解决方法之一是用get()
代替head()
。在这种情况下,我可以使用现有的expectGET()
方法进行测试。但我不确定会有什么缺陷。