And*_*ing 11 javascript underscore.js jasmine angularjs debouncing
我在使用下划线去抖的服务中有一个方法.
在该方法内部是对不同服务的方法的调用.我正在尝试测试调用不同的服务.
在我尝试测试debounced方法时,从不调用不同服务的方法,并且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;
}]);
Run Code Online (Sandbox Code Playgroud)
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();
});
});
Run Code Online (Sandbox Code Playgroud)
为什么AngularJS测试神会离弃我?
*我不知道如何在stackoverflow上给出实际奖励积分,但如果有可能,我会.
Waw*_*awy 16
你只需要模拟lodash debounce方法:
describe('Whoa', function(){
var $injector, whoa, herp;
beforeEach(function(){
module('derp');
spyOn(_, 'debounce').and.callFake(function(cb) { return function() { cb(); } });
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();
});
});
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
7002 次 |
最近记录: |