简体   繁体   中英

How can I stub internally referenced functions with sinonjs?

I have a myModule Node.js module containing:

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

function a() {
  b();
}

exports.a = a
exports.b = b;

And the following test suite using 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;
  })
});

I am expecting the stub to be called, but the original b get called instead, why?

var stub = sinon.stub(object, "method", func);

This has been removed from v3.0.0. Instead you should use stub(object, 'method').callsFake(fn). Or you could use.yields(fn)

https://sinonjs.org/releases/latest/stubs/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM