reactjs - 如何修复使用 moxios 测试 react/redux 应用程序的“网络错误”
问题描述
我正在开发一个包含后端 API 和 react/redux 前端应用程序的新项目。我正在使用 jest/enzyme/moxios 设置我的第一个测试。
我的问题是,当 api 服务器未运行时,测试返回“网络错误”,但如果服务器启动,所有测试都可以。
这是我的笑话测试(CRA):
import React from "react";
import { mount } from "enzyme";
import moxios from "moxios";
import Root from "../Root";
import App from "../App";
beforeEach(() => {
moxios.install();
moxios.stubRequest("*/products", {
status: 200,
response: [
{
id: 0,
name: "Test product 0",
description: "Test description 0",
price: 55.6
},
{
id: 2,
name: "Test product 2",
description: "Test description 2",
price: 55.6
}
]
});
});
afterEach(() => {
moxios.uninstall();
});
it("can fetch a list of products and display them", done => {
const wrapped = mount(
<Root>
<App />
</Root>
);
moxios.wait(() => {
wrapped.update();
console.log(wrapped.find("li").html());
expect(wrapped.find("li").length).toEqual(2);
done();
wrapped.unmount();
});
});
似乎 moxios 没有按预期工作,但我找不到原因......我还检查了 axios 适配器是 moxios:
console.log src/__tests__/productsIntegration.test.js:38
[Function: mockAdapter]
解决方案
推荐阅读
- python - 无法使用 Selenium 单击登录按钮
- android - 不显示 ViewPager 布局
- python - 如何在 PySpark UDF 中使用边缘情况解决分配问题(如匈牙利语/linear_sum_assignment)
- apache-beam - PTransform 传递额外的 PCollection 作为侧输入
- google-apps-script - 谷歌表格 - 搜索范围内的每个单元格,如果数字则过去到另一列
- julia - 带神经偏微分方程的积分微分偏微分方程 - Julia
- python - 在图片上调整 python pcolor 图表
- python - 为什么 send_from_directory 在烧瓶中工作时 render_template 失败?
- github-actions - 通过一个共同的矩阵值“连接”多个作业
- typescript - @typescript-eslint/no-unsafe-assignment - `any` 值的不安全分配