我正在使用Service
测试我的httpBackend
。在这种情况下,这是一个简单的get
请求,我想测试(响应代码)。但我一直在收到错误:
TypeError: undefined is not a function (evaluating '$httpBackend.expectGET('/api/something').response({})') in test/spec/services/requests.js (line 22)
test/spec/services/requests.js:22:51
Error: Unsatisfied requests: GET /api/something in /bower_components/angular-mocks/angular-mocks.js (line 1812)
verifyNoOutstandingExpectation@/bower_components/angular-mocks/angular-mocks.js:1812:74
/test/spec/services/requests.js:28:48
这是服务:
angular.module('requestService', [])
.factory('Request', ['$http', 'localConfig', 'Query', function($http, localConfig){
return {
getRequest: function(fromDate, toDate) {
return $http({
url: '/api/something',
method: "GET",
params: {fromDate: fromDate,
toDate: toDate},
headers: {
'Content-Type': 'application/json; charset=utf-8'
}
})
}
}
}]);
这就是我测试它的方式:
describe('Service: Request', function () {
var reqService, $httpBackend;
beforeEach(module('webapp'));
beforeEach(module('requestService'));
beforeEach(inject(function(Request, _$httpBackend_){
reqService = Request;
$httpBackend = _$httpBackend_;
}));
it('should test the response to be 200', function(){
$httpBackend.expectGET('/api/something').response({});
$httpBackend.flush();
});
afterEach(function(){
$httpBackend.verifyNoOutstandingExpectation();
$httpBackend.verifyNoOutstandingRequest();
})
});
修改
我对我的测试进行了修改(如建议):
it('should test the response to be 200', function(){
console.log('service: ' + reqService);
reqService.getRequest('2016-03-26', '2016-03-29');
$httpBackend.expectGET('/api/something').respond({});
$httpBackend.flush();
});
现在我收到了这个错误:
Error: Unexpected request: GET /api/something?fromDate=2016-03-26&toDate=2016-03-29
Expected GET /api/something in /bower_components/angular-mocks/angular-mocks.js (line 1403)
$httpBackend@/bower_components/angular-mocks/angular-mocks.js:1403:90
sendReq@/bower_components/angular/angular.js:11235:21
serverRequest@/bower_components/angular/angular.js:10945:23
processQueue@/bower_components/angular/angular.js:15552:30
/bower_components/angular/angular.js:15568:39
$eval@/bower_components/angular/angular.js:16820:28
$digest@/bower_components/angular/angular.js:16636:36
flush@/bower_components/angular-mocks/angular-mocks.js:1778:45
/test/spec/services/requests.js:24:23
Error: [$rootScope:inprog] $digest already in progress
http://errors.angularjs.org/1.5.0/$rootScope/inprog?p0=%24digest in bower_components/angular/angular.js (line 17178)
beginPhase@/bower_components/angular/angular.js:17178:88
$digest@//bower_components/angular/angular.js:16616:19
verifyNoOutstandingExpectation@/bower_components/angular-mocks/angular-mocks.js:1810:45
/test/spec/services/requests.js:29:48
答案 0 :(得分:1)
您尚未调用getRequest
服务功能,因此ajax调用永远不会被调用&这就是expectGET
没有得到满足的原因。抛出Error: Unsatisfied requests:
错误。同时修正response
到respond
it('should test the response to be 200', function(){
//action
reqService.getRequest();
$httpBackend.expectGET('/api/something').respond({});
$httpBackend.flush();
});
答案 1 :(得分:0)
我认为您打算使用here,而不是response()
:
$httpBackend.expectGET('/api/something').respond({});