【发布时间】:2019-11-06 04:24:16
【问题描述】:
我有一个非对象函数
关于 getConfig.js
export default function () {
/** do something **/
return {
appConfig: { status: true }
}
}
在我的主文件下
import getConfig from './getConfig';
export default function() {
const { appConfig } = getConfig();
if(appConfig.status) {
console.log("do the right thing");
else {
console.log("do other things")
}
}
}
如何使用 sinon 存根或模拟方法模拟 getConfig 函数?或者有没有更好的方法来设置 appConfig.status 属性为真或假?
我想执行以下操作,但它不起作用
import getConfig from './getConfig';
import main from './main';
test('should status to be true', () => {
sinon.stub(getConfig).callsFake(sinon.fake.returns({status:true});
expect(main()).toBe('to do the right thing')
}
【问题讨论】:
标签: javascript unit-testing sinon