Javascript 如何在后续调用sinon.js存根时使用不同的函数存根

Javascript 如何在后续调用sinon.js存根时使用不同的函数存根,javascript,mocha.js,sinon,Javascript,Mocha.js,Sinon,我正在尝试编写一个测试,根据第一次调用还是第二次调用,我需要将一个函数与一个不同的函数连接起来。到目前为止,我已经尝试: this.dispatcherStub = sinon.stub(alt.dispatcher, 'dispatch'); this.dispatcherStub.onFirstCall().returns((dataArgs) => { // Some assertion on the data }); this.dispatcherStu

我正在尝试编写一个测试,根据第一次调用还是第二次调用,我需要将一个函数与一个不同的函数连接起来。到目前为止,我已经尝试:

  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