【发布时间】:2023-03-29 06:13:01
【问题描述】:
这是我的代码。我想测试“做”方法并验证 this.obj.send 调用计数与时间间隔。
我的问题是当我存根_fetchAllDatas 的方法并解决时。 this.obj.send 无法调用。当this.obj.send 移动到_fetchAllDatas 外面。可以调用。
我该如何处理这个问题?谢谢~
var sinon = require('sinon');
class A {
constructor(obj){
this.obj = obj;
this.timer;
}
doing(){
this.timer = setInterval(()=>{
this._fetchAllDatas().then((data)=>{
console.log('!!!',data);
this.obj.send(data);
});
},1000)
}
stop(){
clearInterval(this.timer);
}
_fetchAllDatas(){
return Promise.resolve([]);
}
}
var sandbox = sinon.sandbox.create();
var spy = { send: sandbox.spy()};
var a = new A(spy);
var stub = sandbox.stub(a,'_fetchAllDatas').resolves(['mark']);
var clock = sandbox.useFakeTimers();
a.doing();;
clock.tick(5000);
sinon.assert.callCount(spy.send, 5); // error the count is zero. it should be 5 times
【问题讨论】:
标签: unit-testing sinon