所以我正在使用 Typescript/RequireJs/Jasmine,并且无法让我的间谍为另一个模块中加载的模块工作。
这是我的规格:
import { CrmWebApiLib } from "../../../webresources/allgnt_/scripts/Allgnt.RestLib";
import { TextFormatter } from "../../../webresources/new_/scripts/script/TextFormatter";
describe("Test", () => {
it("A Test", () => {
spyOn(CrmWebApiLib, "create").and.callFake((a, b) => { });
TextFormatter.test();
expect(CrmWebApiLib.create).toHaveBeenCalled();
});
it("B Test", () => {
var stub = {
CrmWebApiLib: {
create(a, b) { }
}
};
define("../../../webresources/allgnt_/scripts/Allgnt.RestLib", [], stub as any);
spyOn(stub.CrmWebApiLib, "create").and.callFake((a, b) => { });
TextFormatter.test();
expect(CrmWebApiLib.create).toHaveBeenCalled();
});
});
这是文本格式化程序:
import { RestLib, CrmWebApiLib } from "../../../allgnt_/scripts/allgnt.restlib";
export module TextFormatter {
export function test() {
CrmWebApiLib.create("A", "Test");
}
}
在“A 测试”或“B 测试”中,我无法让间谍发挥作用。它总是调用实际的实现和错误。我可以更改它TextFormatter
以允许注入 TextFormatter 的定义,但这感觉不对。
如何让 spyOn 调用在这种情况下工作?