2016-10-22 2 views
2

Ich versuche, eine asynchrone Redux-Aktion zu testen, die einen API-Aufruf mit Axios macht. Ich teste es mit Hilfe von Moxios und es funktionierte vor ein paar Tagen, aber aus irgendeinem Grund heute, als ich eine andere asynchrone Aktion erstellt habe, ist es einfach kaputt gegangen."Kann nicht lesen Eigenschaft 'Daten' undefined" Fehler Wem Einheit testen redux Aktion mit Axios und Moxios

Hier ist meine 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: '[email protected]', 
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 }); 

Hier meine Aktion Schöpfer ist:

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; 

Hier Nachricht mein Mokka Fehler:

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 

Kann mir bitte jemand sagen was ist falsch und wie kann man es reparieren? Danke

Antwort

0

Ich habe einen Weg gefunden, es zu beheben, aber ich weiß immer noch nicht, was es verursacht hat. Dieser Fehler ist zur gleichen Zeit aufgetreten this error passiert ist. Als ich die Zeile browserHistory.push() in redux Aktionserstellern auskommentierte, bestanden alle Tests. Hoffe das hilft jedem.

Verwandte Themen