如何修复使用Moxios测试React / Redux应用程序的“网络错误”

时间:2019-01-08 15:34:05

标签: reactjs redux axios enzyme moxios

我正在研究一个由后端API和react / redux前端应用程序组成的新项目。我正在使用jest / enzyme / moxios设置我的第一个测试。

我的问题是,当api服务器未运行时,测试返回“网络错误”,但如果服务器已启动,则所有测试都可以。

这是我的笑话测试(CRA):

def switchTurnPopUp(grid):
    # Unkown 'state' for this 'grid'
    state = None

    # Get all children 'id' from this 'grid'
    children = grid.children

    # Loop the children 'id'
    for c in children:

        # Get the 'widget' of this 'id'
        widget = children[c]

        # Process only 'Button' widgets
        if isinstance(widget, (Button)):

            # Check the 'state' only once
            if state is None:

                # Get the current 'state' of the first 'Button'
                state = widget.cget('state')

                # Toogle the state
                if state == 'normal':
                    state = 'disabled'
                elif state == 'disabled':
                    state = 'normal'

            # Set the toogled 'state'
            widget.configure(state=state)

moxios似乎没有按预期工作,但我找不到原因...我还检查了axios适配器是否为moxios:

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();
  });
});

0 个答案:

没有答案