我在使用下划线去抖动的服务中有一个方法。
该方法内部是对不同服务上的方法的调用。我正在尝试测试是否调用了不同的服务。
在我尝试测试去抖动方法时,从未调用不同服务的方法,并且 jasmine 失败并显示:
“预期的间谍 aMethod 已被调用。”
我知道它被调用的事实(它在 chrome 中记录到控制台),它只是在期望已经失败之后被调用。
所以......(最好)在不添加 Sinon 或其他依赖项并且
为解决方案提供奖励积分*的情况下,不必将 _.debounce 变成 $timeout...
怎么做?
angular.module('derp', [])
.service('herp', function(){
return {
aMethod: function(){
console.log('called!');
return 'blown';
}
};
})
.service('Whoa', ['herp', function(herp){
function Whoa(){
var that = this;
this.mindStatus = 'meh';
this.getMind = _.debounce(function(){
that.mindStatus = herp.aMethod();
}, 300);
}
return Whoa;
}]);
测试:
describe('Whoa', function(){
var $injector, whoa, herp;
beforeEach(function(){
module('derp');
inject(function(_$injector_){
var Whoa;
$injector = _$injector_;
Whoa = $injector.get('Whoa');
herp = $injector.get('herp');
whoa = new Whoa();
});
});
beforeEach(function(){
spyOn(herp, 'aMethod').andCallThrough();
});
it('has a method getMind, that calls herp.aMethod', function(){
whoa.getMind();
expect(herp.aMethod).toHaveBeenCalled();
});
});
为什么 AngularJS 测试之神抛弃了我?
*我不知道如何在stackoverflow上给出实际的奖励积分,但如果可能的话,我会的。