【发布时间】:2021-09-08 18:05:59
【问题描述】:
我的反应应用程序中有这个组件:
export const DEMO = {
test: {
hi: (txt) => console.log(txt)
}
}
export default function App() {
const getAlert = (c) => {
DEMO.test.hi('hello');
}
return (
<div className="App">
<button onClick={getAlert}>open alert</button>
</div>
);
}
it('should trigger calls', () => {
const spyOnFun = jest.spyOn(DEMO.test, 'hi');
const wrapper = shallow(
<App/>,
);
wrapper.find('button').simulate('click');
expect(spyOnFun).toHaveBeenCalledTimes(1); //expect 1 but get 0
});
你怎么能看到我监视了jest.spyOn(DEMO.test, 'hi'); 方法,我希望在点击后得到 1 个调用,但我得到了 0 并且我的测试没有通过。
有什么问题需要解决?
【问题讨论】:
-
我以前没用过jest,但不应该是 jest.spyOn(DEMO.test.hi, 'hi') 吗? (您缺少 .hi 作为方法名称)
-
我在本地试了一下,测试通过,没有问题。你能发布你的整个测试代码吗?
-
@Ibsn,我试图测试这个逻辑codesandbox.io/s/pk9yb,基本上是下一个想法,用户点击按钮,应该会出现来自
message.info('This is a normal message');的This is a normal message。 Tryngconst spyOnFun = jest.spyOn(message, 'info');我接到 0 个电话。可能是什么问题?