首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用thunk和axios时,Moxios总是返回为空

使用thunk和axios时,Moxios总是返回为空
EN

Stack Overflow用户
提问于 2020-01-03 02:26:40
回答 1查看 121关注 0票数 0

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

LoginAction:

代码语言:javascript
复制
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 });
      });
  };
}

测试:

代码语言:javascript
复制
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);
    });
  });
});

我得到了:

代码语言:javascript
复制
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 []

为什么接收返回为空?

EN

回答 1

Stack Overflow用户

发布于 2020-04-24 13:41:19

在你的快递中你写道

代码语言:javascript
复制
loginUser({email:"test", password:"test"})

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

代码语言:javascript
复制
export function loginUser(email, password) {

我是这样命名它的:

代码语言:javascript
复制
loginUser("test", "test")

希望有人会觉得这很有用

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/59568221

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档