我有一个工厂执行get请求,我想测试。不幸的是,业力测试告诉我没有在$ httpBackend中定义的响应。AngularJS茉莉花测试get-request
AngularJs 1.2.14, 茉莉花2.0, 噶0.12.0
这里是我的模块,我想测试:
var appFactory = angular.module('appFactory', []);
appFactory.factory('boxListService', function($http){
return{
getList: function(){
return $http.get('/boxlist').then(function(result){
return result.data;
});
}
};
});
我的测试是这样的:
describe('Module-Test', function() {
beforeEach(module('appFactory'));
it('should call $http.get in getList', inject(function (boxListService, $httpBackend){
$httpBackend.expectGET('/boxlist');
boxListService.getList();
$httpBackend.flush();
}));
});
消息:
Error: No response defined !
at $httpBackend (D:/nodeJS/host/test_app/src/public/js/libs/angular/angular-mock.js:1206:13)
我看了这个大约5次,我才意识到还需要在expectGET上声明响应。我的印象是,whenGET是唯一需要申报的地方。可以肯定地说,我们必须对whenGET和expectGET两者作出回应吗? – binarygiant