我想确保构造函数在使用Sinon实例化时调用方法,但是,我似乎无法使其工作,因为我相信sinon没有注意到正确的实例化:
class Test {
constructor() {
this.someFunction();
}
someFunction() {
return 1;
}
}
...和测试
describe('constructor', () => {
it('should call someFunction()', () => {
const spyFunc = new Spy(new Test(), 'someFunction');
expect(spyFunc.calledOnce).to.be.true;
});
});
答案 0 :(得分:4)
在调用构造函数之前尝试间谍Test.prototype.someFunction
。
像这样的东西
sinon.spy(Test.prototype, 'someFunction')
const spyFunc = new Test();
expect(spyFunc.someFunction.calledOnce).to.be.true;