【发布时间】:2017-04-02 05:10:51
【问题描述】:
我正在尝试检查是否在我的测试中调用了某个函数。尝试执行此操作时收到错误 TypeError: Cannot read property 'match' of undefined。我已将代码设置为在我的函数上使用sinon.spy(),然后基于此检查callCount。 getMarketLabel 将总是返回一个字符串。以下是我的代码:
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);
})
【问题讨论】:
-
getMarketLabel() 返回什么?要附加一个 sinon spy,您需要执行 sinon.spy(func) 或 sinon.spy(object, "method") 或使用 sinon.spy() 作为函数本身。
-
编辑了原帖。
getMarketLabel将始终返回string -
我觉得这没什么意义,看看怎么用 sinon spies:sinonjs.org/docs/#spies,没有 sinon.spy 方法取字符串。
-
我不是在谈论真正的间谍。所以,我想对函数
getMarketLabel()使用间谍来检查该函数是否已被实际调用。
标签: javascript sinon spy