如何使用 sinon 测试链式函数调用?
How do you test chained function calls with sinon?
我正在测试的代码是:
obj.getTimeSent().getTime();
其中 obj.getTimeSent()
returns 一个日期,然后在该日期调用 getTime()
。
为了存根这个功能,我试过这个:
const timeStub = sandbox.stub(Obj, 'getTimeSent').callsFake(() => {
return 1 //Doesn't matter what it returns
});
sinon documentation 说 callsFake()
"Makes the stub call the provided fakeFunction when invoked" 但是得到一个错误 "TypeError: obj.getTimeSent(...).getTime is not a function"
一个函数应该return链接另一个函数:
const getTimeStub = sandbox.stub().returns(...);
const getTimeSentStub = sandbox.stub(obj, 'getTimeSent').returns({ getTime: getTimeStub });
我正在测试的代码是:
obj.getTimeSent().getTime();
其中 obj.getTimeSent()
returns 一个日期,然后在该日期调用 getTime()
。
为了存根这个功能,我试过这个:
const timeStub = sandbox.stub(Obj, 'getTimeSent').callsFake(() => {
return 1 //Doesn't matter what it returns
});
sinon documentation 说 callsFake()
"Makes the stub call the provided fakeFunction when invoked" 但是得到一个错误 "TypeError: obj.getTimeSent(...).getTime is not a function"
一个函数应该return链接另一个函数:
const getTimeStub = sandbox.stub().returns(...);
const getTimeSentStub = sandbox.stub(obj, 'getTimeSent').returns({ getTime: getTimeStub });