我想检查函数是否在我的测试中被调用过。试图执行此操作时收到错误TypeError: Cannot read property 'match' of undefined
。我已经将我的代码设置为在我的函数上使用sinon.spy()
,然后在此基础上检查callCount
。 getMarketLabel
将总是返回一个字符串。下面是我的代码:sinon spy检查函数是否被调用错误
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间谍,你需要做sinon.spy(func)或sinon.spy(object,“method”)或者使用sinon.spy()作为函数本身。 – DevDig
编辑原文。 'getMarketLabel'将总是返回一个'字符串' – DaveDavidson
我不认为这是有道理的,请参阅如何使用sinon spies:http://sinonjs.org/docs/#spies,没有sinon.spy方法将字符串。 – DevDig