有什么方法可以将调用伪造为sinon间谍?

问题描述 投票:0回答:1

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

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

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

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

但是当我参考文档时我如何获得andCall,我猜想callsFake不能调用spy,我并不认为stud会有类似的行为。

感谢您的任何帮助。

testing chai sinon spy sinon-chai
1个回答
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(testValue).calledWith('hello', 'world')
})

这种存根和调用间谍的方式在我使用Sinon 8.1.1编写的测试中起作用。

© www.soinside.com 2019 - 2024. All rights reserved.