我试图监视 $timeout 以便我可以验证它是否未被调用。具体来说,我的生产代码(见下文)将 $timeout 作为函数调用,而不是对象:
$timeout(function() { ... })
and not
$timeout.cancel() // for instance
然而,Jasmine 需要监视一个对象,如下所示:
spyOn(someObject, '$timeout')
我不知道“someObject”是什么。
我正在使用 Angular 模拟,如果这有什么区别的话。
编辑:我试图测试的相关生产代码如下所示:
EventHandler.prototype._updateDurationInOneSecondOn = function (call) {
var _this = this;
var _updateDurationPromise = this._$timeout(function () {
call.duration = new Date().getTime() - call.startTime;
_this._updateDurationInOneSecondOn(call);
}, 1000);
// ... more irrelevant code
}
在特定的测试场景中,我试图断言 $timeout 从未被调用。
编辑2:明确指定我使用 $timeout 作为函数,而不是对象。
遇到了同样的问题,最终用间谍来装饰 $timeout 服务。
beforeEach(module(function($provide) {
$provide.decorator('$timeout', function($delegate) {
return sinon.spy($delegate);
});
}));
写了更多关于为什么它有效的文章here http://rudickulous.com/post/75310347493/spying-on-angulars-timeout-service.
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)