Javascript 使用sinon如何避免测试嵌套函数?
我用的是摩卡咖啡/柴酒/西诺咖啡,这三种咖啡我都是新手Javascript 使用sinon如何避免测试嵌套函数?,javascript,mocha.js,sinon,sinon-chai,Javascript,Mocha.js,Sinon,Sinon Chai,我用的是摩卡咖啡/柴酒/西诺咖啡,这三种咖啡我都是新手 const a = () => { b(); } const b = () => { console.log('here'); } 在本例中,我只想测试在调用a而不执行b时是否调用了b 比如: it('test', () => { const spy = sinon.spy(b); a(); chai.expect(spy.calledOnce).to.be.true; }) Sinon的存根正是
const a = () => {
b();
}
const b = () => {
console.log('here');
}
在本例中,我只想测试在调用a
而不执行b
时是否调用了b
比如:
it('test', () => {
const spy = sinon.spy(b);
a();
chai.expect(spy.calledOnce).to.be.true;
})
Sinon的
存根
正是您需要的
何时使用存根?
如果要执行以下操作,请使用存根:
it('test', () => {
const stub = sinon.stub(b);
a();
chai.expect(stub.calledOnce).to.be.true;
})