node.js - "Cannot read property ' 数据 ' of undefined"错误 whem 使用 axios 和 moxios 进行单元测试 redux 操作

标签 node.js unit-testing reactjs redux react-router

我正在尝试测试一个异步 redux 操作,该操作使用 axios 进行 api 调用。我正在 moxios 的帮助下测试它,它在几天前工作了,但由于某种原因,今天当我创建另一个异步操作时,它刚刚崩溃了。

这是我的单元测试代码:

  1 import configureMockStore from 'redux-mock-store';
  2 import thunk from 'redux-thunk';
  3 import moxios from 'moxios';
  4
  5 import * as types from '../../../../client/auth/actions/action_types';
  6 import loginAuth from '../../../../client/auth/actions/login_action';
  7
  8 require('dotenv').config();
  9 const expect = require('chai').expect;
 10
 11 const mockStore = configureMockStore([thunk]);
 12
 13 const user = {
 14   email: 'john@loginActionTest.com',
 15   password: 'secret',
 16 };
 17
 18 describe('authAction -> loginUser() ', () => {
 19   beforeEach(() => {
 20     moxios.install();
 21   });
 22
 23   afterEach(() => {
 24     moxios.uninstall();
 25   });
 26
 27   it('should create AUTH_SUCCESS when finishes without error', () => {
 28     const store = mockStore({});
 29     const token = 'authToken';
 30     const expectedActions = [
 31       { type: types.AUTH_REQUEST },
 32       { type: types.AUTH_SUCCESS, token },
 33     ];
 34
 35     moxios.wait(() => {
 36       const request = moxios.requests.mostRecent();
 37       request.respondWith({
 38         status: 200,
 39         response: { success: true, token },
 40       });
 41     });
 42
 43     return store.dispatch(loginAuth(user)).then(() => {
 44       expect(store.getActions()).to.deep.equal(expectedActions);
 45     });
 46   });

这是我的 Action 创建者:

  1 /* @flow */
  2 import axios from 'axios';
  3 import {
  4   authRequest,
  5   authSuccess,
  6   authError,
  7 } from './auth_actions';
  8
  9 function loginAuth(user: { email: string, password: string }) {
 10   return function (dispatch: any) {
 11     dispatch(authRequest());
 12
 13     return axios.post('/api/auth/login', {
 14       email: user.email,
 15       password: user.password,
 16     })
 17     .then((response: any) => {
 18       dispatch(authSuccess(response.data.token));
 19     })
 20     .catch((error: any) => {
 21       dispatch(authError(error.response.data.messages));
 22     });
 23   };
 24 }
 25
 26 export default loginAuth;

这是我的摩卡错误消息:

authAction -> loginUser()  should create AUTH_SUCCESS when finishes without error:
     TypeError: Cannot read property 'data' of undefined
      at client/auth/actions/login_action.js:21:26

有人可以告诉我出了什么问题以及如何解决吗?谢谢

最佳答案

我已经找到了解决这个问题的方法,但我仍然不知道是什么原因造成的。这个错误同时发生this error发生了。当我在 redux 操作创建者中注释掉 browserHistory.push() 行时,所有测试都通过了。希望这对任何人都有帮助。

关于node.js - "Cannot read property ' 数据 ' of undefined"错误 whem 使用 axios 和 moxios 进行单元测试 redux 操作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40192527/

相关文章:

java - dropwizard 中的单元测试 View

css - 样式化 vanilla html 表格以与样式组件 react

javascript - 如何在 Watson Conversation 中保存数据?

java - 对仅调用 lambda 表达式链中的其他方法的 java 方法进行单元测试

unit-testing - 如何从 Angular -cli 4 单元测试中的输入模拟按键事件

reactjs - 您如何将 Next.js dynamic 与导出多个函数的库一起使用?

javascript - React,未生成Webpack : bundle. js

node.js - 尝试从应用程序服务中的子目录运行 Node 应用程序提供 server.js 文件

node.js - 使用 Bazel 构建 Node.Js Docker 镜像

javascript - 环回模型在保存之前删除属性或属性