【发布时间】:2021-10-21 20:24:13
【问题描述】:
我正在尝试在我的文件中存根所需的函数。例如:
fileA.js
function doSomething() {
return 5;
}
modules.export = { doSomething }
fileB.js
const { doSomething } = require('./fileA');
function doAnotherThing() {
let anotherThing = 10;
return anotherThing + doSomething();
}
modules.export = { doAnotherThing }
在我的测试文件中:fileBTest.js
const fileA = require('./fileA');
const fileB = require('./fileB');
describe('Test File B', function(){
it('Example of failing stub', function() {
const fileAStub = sinon.stub(fileA, 'doSomething');
fileAStub.returns(15);
expect(fileB.doAnotherThing()).to.equal(25);
})
})
但是,我的 fileAStub 无法正常工作,因为在 fileB.js 中,它是一个函数所必需的,而不是整个 fileA 所必需的。
如果我将 fileB.js 中的 fileA 要求更改为以下方式,我的存根将起作用。
updatedFileB.js
const fileA = require('./fileA');
function doAnotherThing() {
let anotherThing = 10;
return anotherThing + fileA.doSomething();
}
问题:我如何存根 { doSomething } 而不是更改我在 fileB 中要求的方式?
【问题讨论】:
标签: node.js unit-testing chai sinon