1 回答

TA貢獻1796條經驗 獲得超10個贊
你otherFile.buildSomeThing是異步的,你需要在檢查privFunc存根是否被調用之前等待它。
例如:
文件.js
let otherFile = require('otherFile')
var privFunc = function(data) {
}
var sampleFunc = function() {
return otherFile.buildSomeThing.then(function(data) {
privFunc(data)
})
}
module.exports = {sampleFunc}
文件.test.js
var fileController = rewire('./file')
var stub = sinon.stub().returns("abc")
fileController.__set__('privFunc', stub)
fileController.sampleFunc().then(() => {
expect(stub).to.have.been.called;
});
如果你使用 mocha,你可以使用這樣的東西:
describe('file.js test cases', () => {
let stub, reset;
let fileController = rewire('./file');
beforeEach(() => {
stub = sinon.stub().returns("abc");
reset = fileController.__set__('privFunc', stub);
});
afterEach(() => {
reset();
});
it('sampleFunc calls privFunc', async () => {
await fileController.sampleFunc();
expect(stub).to.have.been.called;
});
});
添加回答
舉報