[英]Angular Karma Unit Testing Controller Not Injecting Mocked Service Dependency
I have tried various ways of instantiating the mocked service 'listFilterManager' and passing this to the controller. 我尝试了各种方法来实例化模拟服务“ listFilterManager”并将其传递给控制器。 Although the mocked service is created and accessible, when the scope.$digest() is executed, the actual non-mocked service runs instead of the mocked listFilterManager. 尽管已创建并访问了模拟服务,但是当执行scope。$ digest()时,将运行实际的非模拟服务,而不是模拟listFilterManager。 Even more confusing is that the resourceStore service is properly mocked and injected. 更令人困惑的是,resourceStore服务已正确模拟和注入。 Any help would be greatly appreciated. 任何帮助将不胜感激。
Relevant code: 相关代码:
beforeEach(function(){
module('listModule');
module(function($provide){
$provide.factory('resourceStore', function(){
return {
alertsArray: Object.keys(mockData).map(function(key){
return mockData[key];
})
};
});
$provide.value('listFilterManager',{
filterList: jasmine.createSpy('filterList').and.callFake(function(input){
return input;
})
});
});
});
describe('instantiates the Alert List Ctrl', function(){
var scope, listFilterParams, listFilterManager, resourceStore, tableAccessorDictionaries, AlertListCtrl;
beforeEach(function(){
inject(function(_$rootScope_, $controller, _tableAccessorDictionaries_, _listFilterParams_, _resourceStore_, _listFilterManager_){
scope = _$rootScope_.$new();
listFilterParams = _listFilterParams_;
listFilterManager = _listFilterManager_; //this refs the mocked service, but the actual service is the one that is executed.
var deps = {
'$scope': scope,
'tableAccessorDictionaries': _tableAccessorDictionaries_,
'listFilterManager': _listFilterManager_,
'listFilterParams': _listFilterParams_,
'resourceStore': _resourceStore_
};
AlertListCtrl = $controller('AlertListCtrl', deps);
});
});
it('it should filter the list when a list filter parameter changes', function(){
expect(listFilterManager.filterList).not.toHaveBeenCalled();
scope.filterParams.text = 'This is a test query';
scope.$digest();
expect(listFilterManager.filterList).toHaveBeenCalled();
});
});
So you're saying that 'listFilterManager' is a service but in the beforeEach method, you are providing it as a value. 因此,您说的是“ listFilterManager”是一项服务,但是在beforeEach方法中,您将其作为值提供。 You need to provide it as a factory in order for it to be properly mocked. 您需要将其作为工厂提供,以便对其进行正确模拟。 Replace the following 替换以下
$provide.value('listFilterManager',{
filterList: jasmine.createSpy('filterList').and.callFake(function(input){
return input;
})
});
with: 与:
$provide.factory('listFilterManager', function () {
return {
filterList: jasmine.createSpy('filterList').and.callFake(function(input){
return input;
};
})
});
and it should work properly. 它应该可以正常工作。
After some time I figured it out. 一段时间后,我想通了。 The issue was because the function I was testing was called from within an underscore.debounce wrapper. 问题是因为我正在测试的功能是从underscore.debounce包装器中调用的。 Debounce apparently causes testing issues, as described here . 防抖动显然会导致测试问题,如所描述这里 。 The solution is to redefine _.debounce within the test file as such (credit to the aforementioned post): 解决方案是这样在测试文件中重新定义_.debounce(贷记至上述文章):
_.debounce = function (func) { return function () { func.apply(this, arguments);}; };
Whew, what a frustrating issue, but hopefully this will help someone else in need! 哦,这是一个令人沮丧的问题,但是希望这可以帮助需要帮助的其他人!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.