首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用axios和moxios对redux操作进行单元测试时出现"Cannot read property 'data‘of undefined“错误

使用axios和moxios对redux操作进行单元测试时出现"Cannot read property 'data‘of undefined“错误
EN

Stack Overflow用户
提问于 2016-10-22 21:06:32
回答 1查看 1.1K关注 0票数 2

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

以下是我的单元测试代码:

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

这是我的动作创建者:

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

以下是我的mocha错误消息:

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

有没有人能告诉我哪里出了问题,以及如何修复?谢谢

EN

回答 1

Stack Overflow用户

发布于 2016-10-23 00:12:31

我已经找到了修复它的方法,但是我仍然不知道是什么原因造成的。此错误与this error同时发生。当我在redux action creators中注释掉行browserHistory.push()时,所有的测试都通过了。希望这对任何人都有帮助。

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

https://stackoverflow.com/questions/40192527

复制
相关文章

相似问题

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