问题描述
我已经在react native中构建了一个应用程序,现在正在经历更新一些旧测试套件的过程。发生错误的测试是使用React Test Renderer模拟屏幕功能的组件测试。
错误摘要:Expect语句之一抛出错误提示
无法读取未定义的属性“调用”
当值存在时,我可以像这样打印出属性调用
console.log(store.dispatch.mock.calls[6][0]);
它给了我期望值。
代码:
//Imports
jest.spyOn(Date,"now").mockImplementation(() => 1479427200000);
const mockStore = configureStore([]);
describe("block1",() => {
it("test1",async done => {
try {
let component;
let store = mockStore(stores.SummaryR1);
store.dispatch = await jest.fn();
const mockDate = new Date(1466424490000);
const spy = jest.spyOn(global,"Date").mockImplementation(() => mockDate);
Date.now = jest.fn(() => 1466424490000);
await act(async () => {
component = await renderer.create(
<PaperProvider>
<Provider store={store}>
<Receive_Signature />
</Provider>
</PaperProvider>
);
});
const expected = await component.toJSON();
expect(expected).toMatchSnapshot();
await act(async () => {
//action
});
await act(async () => {
//action
});
await act(async () => {
//action
});
await act(async () => {
//action
});
await act(async () => {
//action
});
expect(store.dispatch).toHaveBeenCalledTimes(8);
expect(store.dispatch).toHaveBeenNthCalledWith(1,{results1});
expect(store.dispatch).toHaveBeenNthCalledWith(2,{results2});
expect(store.dispatch).toHaveBeenNthCalledWith(3,{results3});
expect(store.dispatch).toHaveBeenNthCalledWith(4,{results4});
expect(store.dispatch).toHaveBeenNthCalledWith(5,{results5});
expect(store.dispatch).toHaveBeenNthCalledWith(6,{results6});
expect(store.dispatch).toHaveBeenNthCalledWith(7,{results7} );
expect(store.dispatch).toHaveBeenNthCalledWith(8,{results8});
expect(navigateToScreen).toHaveBeenCalledTimes(1);
expect(navigateToScreen.mock.calls[0][0]).toEqual("Processor_Dashboard");
done();
} catch (error) {
done.fail(error);
}
},15000);
第7次调用的测试结果7上出现了错误。
首先,我知道有8个电话
expect(store.dispatch).toHaveBeenCalledTimes(8);
然后,我还可以打印出result7,然后看我是否有关于result7的正确数据。但是当我在开玩笑的期望语句中运行它时,我得到了错误:
无法读取未定义的属性“调用”
我不知道为什么会出现此错误,因为其他所有期望都将运行,并且如果我仅注释掉一条语句,那么套件的其余部分也将正常运行。由于某种原因,它只是在一个Expect语句上出错。
显然,数据已被删除,但不影响其运行方式。
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)