有什么方法可以将调用伪造与sinon间谍一起使用吗?

时间:2019-12-13 16:29:30

标签: testing chai sinon spy sinon-chai

我正在尝试从expect迁移到柴和锡南。 我希望我们做这样的事情

this.check = expect.spyOn(module, "method").andCall(function(dep) {
    return dep;
});

但是我想要柴和诗乃的这种行为。

 this.check = sinon.spy(module, "method")

但是当我参考文档时如何获得andCall,我猜不能用callsFake来调用spy,而且我真的不认为{ {1}}。

感谢您的帮助。

1 个答案:

答案 0 :(得分:0)

您可以执行以下操作:

it('makes blue cheese', () => {
  const mySpy = sinon.spy()

  sinon.stub(module, 'method')
    .callsFake((...args) => {
      mySpy(...args)
      // Do other stuff here like return a value
      return 'blue cheese'
    })

  const testValue = module.method('hello', 'world')
  expect(testValue).to.eq('blue cheese')
  expect(mySpy).to.have.been.calledWith('hello', 'world')
})

这种对间谍的侦听和调用方式在我使用Sinon 8.1.1编写的测试中有效。

相关问题