【发布时间】:2016-08-04 09:52:50
【问题描述】:
我有两个包含导出函数的模块。 “ModuleB”使用“ModuleA”中的功能。现在我想测试“ModuleB”并模拟“ModuleA”中使用的函数。
我将 ES6 与 babel 一起使用。对于测试,我使用 karma 和 jasmine。
我尝试使用 babel-rewire 和 inject-loader,但它不起作用。我对这一切有点陌生,我想我只是做错了什么。因此,感谢您的帮助!
moduleA.js
export function getResult() {
return realResult;
}
moduleB.js
import * as ModuleA from './moduleA'
export function functionToTest() {
let result = ModuleA.getResult();
// do stuff with result which I want to test
}
我的测试
it("does the right thing", () => {
// I tried using rewire, but this gives me an exception:
ModuleB.__Rewire__('ModuleA', {
getResult: () => {
return fakeResult;
}
});
let xyz = ModuleB.functionToTest(canvas, element);
});
【问题讨论】:
标签: javascript unit-testing jasmine ecmascript-6