AngularJS“;“无待处理的刷新请求”;在使用$resource对控制器进行单元测试时
我正在为控制器编写单元测试。此控制器已注入$resource服务:AngularJS“;“无待处理的刷新请求”;在使用$resource对控制器进行单元测试时,angularjs,unit-testing,jasmine,Angularjs,Unit Testing,Jasmine,我正在为控制器编写单元测试。此控制器已注入$resource服务: function controller($scope, Service) { Service.get(function(result){ // do stuff with the result, not relevant here } } 服务的定义如下: angular.module('so').factory('Service', ['$resource', service]); funct
function controller($scope, Service) {
Service.get(function(result){
// do stuff with the result, not relevant here
}
}
服务的定义如下:
angular.module('so').factory('Service', ['$resource', service]);
function service($resource) {
return $resource('/url', null, {
get: { method: 'POST', params: {}, isArray: false}
});
}
我的Jasmine单元测试如下:
describe("Controller", function(){
var $httpBackend;
beforeEach(function() {
module('so');
inject(function( _$httpBackend_) {
$httpBackend = _$httpBackend_;
});
});
it('should have done stuff irrelevant to the question', function() {
var $injector = angular.injector('so'),
$scope = $injector.get('$rootScope'),
$httpBackend
.whenPOST('/url')
.respond ([]);
// controller needs to be defined here and not in the beforeEach as there
// are more parameters passed to it, depending on the test
var controller = $injector.get('$controller')('controller', { "$scope": $scope });
$httpBackend.flush();
// then here the actual test resolution, also irrelevant
});
});
我在运行测试时出错:
Error: No pending request to flush ! in file:///path/to/angular-mock.js (line 1453)
我在Service.get()的回调中添加了一个console.log(),实际上,它没有被调用(当然,回调之外的所有内容都被调用)。正如我在另一个问题中看到的那样,如果在单元测试中创建控制器之后没有阶段化,也尝试添加范围摘要,但没有成功
我知道我可以用其他一些方法模拟它,但是使用$httpBackend似乎是测试的完美解决方案:模拟Web服务器和接收的数据
我正在使用AngularJS 1.2.16(无法升级到1.3.*,需要IE 8兼容性)。我首先使用了1.2.13,并进行了更新,以检查它是否能够解决问题,但没有任何运气 这是一个注射问题,通过将测试从
it('should have done stuff irrelevant to the question', function() {
var $injector = angular.injector('so'),
$scope = $injector.get('$rootScope'),
$httpBackend
.whenPOST('/url')
.respond ([]);
// controller needs to be defined here and not in the beforeEach as there
// are more parameters passed to it, depending on the test
var controller = $injector.get('$controller')('controller', { "$scope": $scope });
$httpBackend.flush();
// then here the actual test resolution, also irrelevant
});
致:
基本上,在测试函数中添加inject()并“手动”将服务传递给控制器
我发现了问题,这很好,但我真的不明白为什么它不起作用。此外,在找到解决方案后,我立即尝试了以下方法:
it('should have done stuff irrelevant to the question', inject(function() {
// edited lines because they did not change
var Service = $injector.get('Service'),
var controller = $injector.get('$controller')('controller', { "$scope": $scope, "Service": Service });
// edited lines because they did not change
}));
但这再次失败,出现了相同的“无未决请求”错误。我猜这是一个赛车问题,我的服务在之后创建时无法获得适当的$httpBackend注入,但我真的不明白为什么会发生这种情况。如果有人能启发我。。。我会很感激的
it('should have done stuff irrelevant to the question', inject(function() {
// edited lines because they did not change
var Service = $injector.get('Service'),
var controller = $injector.get('$controller')('controller', { "$scope": $scope, "Service": Service });
// edited lines because they did not change
}));