【问题标题】:How to spy with sinon on callback calls triggered by EventEmitter events? Javascript, ES6, Unit-tests, Chai如何用 sinon 监视由 EventEmitter 事件触发的回调调用? Javascript,ES6,单元测试,Chai
【发布时间】:2018-11-28 03:49:31
【问题描述】:

我需要测试我的回调是否被调用了 n 次并且总是返回 true。
这是我在打字稿中的测试回调函数:

const checkBlockTransaction = (block: ILogsBlock) => {
  const tx = transactions.find(element => element.block === block.blockNumber);
  try {
    assert.strictEqual(block.transactions[0].amount, tx.amount);
  } catch (e) {
    return false;
  }
  return true;
};

这是我目前失败的测试,因为间谍没有注册任何函数调用

describe('Erc20DepositsWatcher', () => {
  it('handles blocks correctly', async () => {
    const spy = sinon.spy(checkBlockTransaction);
    for (const tx of transactions) {
      await deployedContract.methods.transfer(tx.address, tx.amount)
      .send({ from: addresses[0] });
    }

    depositsWatcher.subscribe(checkBlockTransaction);
    await depositsWatcher.startBroadcasting();
    await depositsWatcher.handleNewBlock(await web3.eth.getBlock('latest'));
    assert.equal(spy.callCount, 7);
    //sinon.assert.callCount(spy, 7);
    //assert(spy.alwaysReturned(true));
  });
});  

也许有比用 sinon 监视更好的解决方案,但我还没找到

【问题讨论】:

    标签: javascript ecmascript-6 sinon chai spy


    【解决方案1】:

    我目前的解决方案是没有间谍,但看起来不是很好:

    function checkCallbackCalled(done: any, callsNumber: number) {
      let counter = 0;
      return (block: ILogsBlock) => {
        const tx = transactions.find(element => element.block === block.blockNumber);
        try {
          assert.strictEqual(block.transactions[0].amount, tx.amount);
        } catch (e) {
          done(e);
        }
        counter += 1;
        if (counter === callsNumber) done();
      };
    }
    
    describe('Erc20DepositsWatcher', () => {
      it('handles blocks correctly', async (done) => {
        for (const tx of transactions) {
          await deployedContract.methods.transfer(tx.address, tx.amount)
          .send({ from: addresses[0] });
        }
    
        depositsWatcher.subscribe(checkCallbackCalled(done, 7));
        await depositsWatcher.startBroadcasting();
        await depositsWatcher.handleNewBlock(await web3.eth.getBlock('latest'));
      });
    });
    

    【讨论】:

      猜你喜欢
      • 2013-03-12
      • 2019-07-12
      • 2017-09-25
      • 1970-01-01
      • 2020-05-23
      • 2023-04-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多