[英]How can I stub internally referenced functions with sinonjs?
我有一个myModule Node.js 模块,其中包含:
function b() {
console.log('original b');
}
function a() {
b();
}
exports.a = a
exports.b = b;
以下测试套件使用 mocha + sinon.js:
const myModule = require('./myModule.js');
const sinon = require('sinon');
const sinonChai = require('sinon-chai');
chai.use(sinonChai);
describe('not working stub', () => {
it('should call the stub', () => {
let stub = sinon.stub(myModule, 'b', () => { console.log('stubbed b')});
myModule.a();
expect(stub).to.have.been.called;
})
});
我期待存根被调用,但原来的 b 却被调用了,为什么?
var stub = sinon.stub(object, "method", func);
这已从 v3.0.0 中删除。 相反,您应该使用 stub(object, 'method').callsFake(fn)。 或者你可以使用.yields(fn)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.