Sinon Stub/Spy使用WithArgs不按预期行事

Jos*_*ltz 4 sinon

当我为一个sinon spy或stub指定withArgs时,我希望callCount只计算带有这些参数的调用.但这似乎并没有发生.

如果我运行以下内容:

var mySpy = sinon.spy();
mySpy.withArgs("foo");

mySpy("bar");

expect(mySpy.callCount).to.be(0);
Run Code Online (Sandbox Code Playgroud)

我得到"预期1等于0".我疯了,这是一个错误,还是有另一种方法可以做到这一点?

Jos*_*ltz 6

您还必须在断言中添加withArgs,如下所示:

var mySpy = sinon.spy();
mySpy.withArgs("foo");

mySpy("bar");

expect(mySpy.withArgs("foo").callCount).to.be(0);
Run Code Online (Sandbox Code Playgroud)

  • 或者使用类似的模拟:`this.mock().withArgs("foo")`在自动验证模拟的上下文中,或者您可以手动验证它们. (2认同)