简体   繁体   English

错误无法使用axios和moxios来测试Redux动作的Whem单元测试“无法读取未定义的属性'data'”

[英]“Cannot read property 'data' of undefined” error whem unit testing redux action with axios and moxios

I am trying to test an async redux action that makes an api call with axios. 我正在尝试测试使用axios进行api调用的异步redux动作。 I am testing it with the help of moxios and it worked a couple of days ago but for some reason today when i created another async action it just broke. 我在moxios的帮助下对其进行了测试,并且该方法已在几天前生效,但是由于今天的某些原因,当我创建另一个异步操作时,它刚刚崩溃了。

Here is my unit test code: 这是我的单元测试代码:

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

Here is my action creator: 这是我的动作创建者:

  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;

Here is my mocha error message: 这是我的摩卡错误消息:

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

Can someone please tell me what is wrong and how to fix it? 有人可以告诉我哪里出了问题以及如何解决吗? Thank you 谢谢

I have found a way to fix it however I still don't know what caused it. 我已经找到一种解决方法,但是我仍然不知道是什么原因造成的。 This error happened at the same time this error happened. 发生此错误的同时发生了此错误 When I commented out the line browserHistory.push() in redux action creators all the tests passed. 当我注释掉redux操作创建者中的browserHistory.push()行时,所有测试都通过了。 Hope this helps anyone. 希望这对任何人有帮助。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 摩卡单元测试:未捕获的类型错误:无法读取未定义的属性“应用” - Mocha Unit Testing: Uncaught TypeError: Cannot read property 'apply' of undefined 我在 axios 补丁 api 中收到“Uncaught (in promise) TypeError: Cannot read property 'data' of undefined”错误 - I'm getting “Uncaught (in promise) TypeError: Cannot read property 'data' of undefined” error in axios patch api Axios 发布错误类型错误:无法读取未定义的属性“创建” - Axios posting error TypeError: Cannot read property 'create' of undefined 无法读取未定义的属性“操作” - Cannot read property 'action' of undefined 通用Redux:浏览器历史记录错误:无法读取未定义的属性“侦听” - Universal redux: browserhistory error: cannot read property 'listen' of undefined 使用supertest在Nodejs上进行测试会给出“无法读取未定义的属性'header'”错误 - Testing on Nodejs with supertest gives “Cannot read property 'header' of undefined” error 使用 axios 渲染 ejs 时出现“无法读取数据属性错误” - "cannot read data property error" when rendering ejs with axios React / Redux-无法读取未定义的属性“ XXX” - React / Redux - Cannot read property “XXX” of undefined 类型错误:无法使用 JEST 单元测试读取 NodeJS 中未定义的属性“应用” - TypeError: Cannot read property 'apply' of undefined in NodeJS using JEST unit testing 无法读取未定义的属性“ on”? ->错误 - Cannot read property 'on' of undefined? -> ERROR
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM