【发布时间】:2018-09-02 14:10:08
【问题描述】:
我熟悉在类或对象方法上设置间谍,但是当函数只是一个导出默认值时怎么办 - 这样方法本身是独立的,就像一个实用程序一样?
我有一些像这样的现有代码:
const Funct1 = props => {
if(props){
Funct2(args);
}
// or return something
};
const Funct2 = props => {
// do something
return true
};
export default Funct1; //Yes the existing export is named the same as the "entry" method above.
例如,我想监视Funct1 被调用,Funct2 返回 true。
import Funct1 from "../../../src/components/Funct1";
describe("Test the Thing", () => {
it("New Test", () => {
let props = {
active: true,
agentStatus: "online"
};
const spy = spyOn(Funct2, "method name"); <-- how doe this work if not an obj or class?
Funct1(props);
//If I try Funct2(props) instead, terminal output is "Funct2 is not defined"
expect(spy).toHaveBeenCalledWith(props);
});
});
【问题讨论】:
-
我注意到这没有被接受的答案。仅供参考,我尝试
import * as functions,然后创建一个const spy = jest.spyOn(functions, 'name')——但这对我不起作用。
标签: javascript jasmine jestjs