sinon spy检查函数是否被调用错误

时间:2016-11-18 11:08:40

标签: javascript sinon spy

我正在尝试检查我的测试中是否已调用某个函数。尝试执行此操作时收到错误TypeError: Cannot read property 'match' of undefined。我已将我的代码设置为在我的函数上使用sinon.spy(),然后根据此检查callCountgetMarketLabel始终返回一个字符串。以下是我的代码:

beforeEach(() => {
  marketLabelSpy = sinon.spy(getMarketLabel());
}); //please note this is in a describe block but didnt feel it was relevant to post it. marketLabelSpy is pre-defined.

it('should be called', () => {
  expect(marketLabelSpy).to.have.callCount(1);
})

1 个答案:

答案 0 :(得分:1)

在你的代码中,你正在调用getMarketLabel函数,函数调用的结果(这是一个字符串)将用于设置你的间谍。这不符合您的预期。

要在函数上使用sinon间谍,只需传递对该函数的引用:

beforeEach(() => {
  marketLabelSpy = sinon.spy(getMarketLabel);
});
相关问题