繁体   English   中英

我怎样才能用 sinonjs 存根内部引用的函数?

[英]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)

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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