使用 sinon 如何避免测试嵌套函数?
Using sinon how to avoid testing nested function?
我正在使用 mocha/chai/sino,我是其中三个的新手。
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;
})
诗乃的stub
就是你要找的
When to use stubs?
Use a stub when you want to:
Control a method’s behavior from a test to force the code down a specific path. Examples > include forcing a method to throw an error in order to test error handling.
When you want to prevent a specific method from being called directly (possibly because it triggers undesired behavior, such as a XMLHttpRequest or similar).
it('test', () => {
const stub = sinon.stub(b);
a();
chai.expect(stub.calledOnce).to.be.true;
})
我正在使用 mocha/chai/sino,我是其中三个的新手。
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;
})
诗乃的stub
就是你要找的
When to use stubs? Use a stub when you want to:
Control a method’s behavior from a test to force the code down a specific path. Examples > include forcing a method to throw an error in order to test error handling.
When you want to prevent a specific method from being called directly (possibly because it triggers undesired behavior, such as a XMLHttpRequest or similar).
it('test', () => {
const stub = sinon.stub(b);
a();
chai.expect(stub.calledOnce).to.be.true;
})