我如何使用sinonjs从Node.js模块中导出函数?

我有一个myModule Node.js模块,其中包含:

function b() { console.log('original b'); } function a() { b(); } exports.a = a exports.b = b; 

下面的testing套件使用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被调用,为什么呢?