0

我一直试图模拟我的 axios 函数,该函数总是返回空:

登录操作:

export function loginUser(email, password) {
  return function(dispatch) {
    return axios
      .post(`${API_URL}/users/authenticate`, { email, password }, { withCredentials: true })
      .then(response => {
          localStorage.setItem("token", JSON.stringify(response.data.user.id));
          dispatch(setupUser(response.data.user.id));
          dispatch({ type: AUTH_USER });
          dispatch({ type: CLEAR_LOGIN_MESSAGE });
      });
  };
}

测试:

describe("async actions", () => {
  beforeEach(() => {
    moxios.install();
  });
  afterEach(() => {
    moxios.uninstall();
  });

  it("logs in", () => {
    moxios.wait(() => {
      const request = moxios.requests.mostRecent();
      request.respondWith({
        status: 200,
        response: {name:"Jill", id:1},
      });
    });

    const expectedActions = [
      { type: AUTH_USER },
      { type: CLEAR_LOGIN_MESSAGE },
      { type: SET_UP_USER, information: {name:"Jill, id:1} },
      { type: REFRESH_DASHBOARD, dashboard: "" },
    ];

    const store = mockStore({});
    return store.dispatch(loginUser({email:"test", password:"test"})).then(() => {
      expect(store.getActions()).toEqual(expectedActions);
    });
  });
});

我得到:

expect(received).toEqual(expected) // deep equality

- Expected
+ Received

- Array [
-   Object {
-     "type": "AUTH_USER",
-   },
-   Object {
-     "type": "CLEAR_LOGIN_MESSAGE",
-   },
-   Object {
-     "information": 
-       "id": "1",
-       "name": "Jill",
-     },
-     "type": "SET_UP_USER",
-   },
-   Object {
-     "dashboard": "",
-     "type": "CLEAR_DASHBOARD",
-   },
- ]
+ Array []

为什么received返回是空的?

4

1 回答 1

2

在您的派遣中,您写道

loginUser({email:"test", password:"test"})

但是您的操作不需要对象,它需要 2 个参数

export function loginUser(email, password) {

我通过这样称呼它来让它工作:

loginUser("test", "test")

希望有人觉得这很有用

于 2020-04-24T05:41:18.790 回答