我正在使用 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;
})
最佳答案
Sinon 的 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;
})
关于javascript - 使用sinon如何避免测试嵌套函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62946180/