[英]How to stub with a different function on subsequent calls to sinon.js stub
我正在尝试编写一个测试,我需要根据它是第一次被调用还是第二次调用不同的函数来对一个函数进行存根。 到目前为止,我已经尝试过:
this.dispatcherStub = sinon.stub(alt.dispatcher, 'dispatch');
this.dispatcherStub.onFirstCall().returns((dataArgs) => {
// Some assertion on the data
});
this.dispatcherStub.onSecondCall().returns((dataArgs) => {
// Another assertion on the data
done();
});
请注意,我需要它们是不同的函数,而不仅仅是不同的返回不同的值,因为我需要在第二个函数中调用 mocha 的 done(),因为它被异步调用。
您需要执行返回的函数:
this.dispatcherStub = sinon.stub(alt.dispatcher, 'dispatch');
this.dispatcherStub.onFirstCall().returns(
(function () {}();
});
this.dispatcherStub.onSecondCall().returns((dataArgs) => {
(function () {
done();
}();
});
您还可以使用(() => return 4)();
将箭头函数转换为 IIFE (() => return 4)();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.