【问题标题】:Jest - Test gives an error TypeError: Cannot read property 'then' of undefined开玩笑 - 测试给出错误 TypeError: Cannot read property 'then' of undefined
【发布时间】:2018-11-01 05:30:13
【问题描述】:

当我运行测试时,它给了我一个错误 TypeError: Cannot read property 'then' of undefined,我试图在 Docs 上寻找一些修复,但我没有找到解决这个问题的方法,任何想法非常感谢如何解决此问题。

contact-actions.js

 export function getContacts() {
      return function(dispatch) {
        dispatch({type: 'GET_CONTACTS_PENDING'})
        axios.get('http://127.0.0.1:8000/api/contacts', { 
        })
          .then((response) => {
            dispatch({type: 'GET_CONTACTS', payload: response.data})
            dispatch(hideLoading())
          })
          .catch((error) => {
            dispatch({type:  'CONTACT_ERROR', payload: error})
        })
      }
    }


    app.spec.js

    import React from 'react';
    import { shallow, mount } from 'enzyme';
    import renderer from 'react-test-renderer';
    import MockAdapter from 'axios-mock-adapter';
    import configureMockStore from 'redux-mock-store';
    import nock from 'nock';
    import axios from 'axios';
    import moxios from 'moxios';
    import expect from 'expect';
    import thunk from 'redux-thunk';
    import { Provider } from 'react-redux';
    import * as actions from '../src/actions/contact-actions';
    import * as types from '../src/constants/action-types';

    const middlewares = [thunk];
    const mockStore = configureMockStore(middlewares);

    describe('async actions', () => {
      afterEach(() => {
        nock.cleanAll();
      });

      it('creates GET_CONTACTS when fetching contacts has been done', () => {
        const store = mockStore({});

        const contacts = {};

        nock('http://127.0.0.1:8000')
          .get('/api/contacts')
          .reply(200, { body: {  contacts: [{ first_name: 'shadow', last_name: 'madow', phone: 5566, email: 'shadow@yahoo.com' }] } });

        const expectedActions = [
          { type: types.GET_CONTACTS, body: {  contacts: [{ first_name: 'shadow', last_name: 'madow', phone: 5566, email: 'shadow@yahoo.com' }] }}
        ];

        return store.dispatch(actions.getContacts()).then(() => {
          // return of async actions
          expect(store.getActions()).toEqual(expectedActions);
        });
      });
    });

【问题讨论】:

    标签: javascript reactjs react-redux axios


    【解决方案1】:

    确保从 getContacts() 中的匿名函数返回承诺:

    export function getContacts() {
      return function(dispatch) {
        dispatch({type: 'GET_CONTACTS_PENDING'})
        axios.get('http://127.0.0.1:8000/api/contacts', { // THIS LINE
        })
        ...
    

    改变这个:

    axios.get('http://127.0.0.1:8000/api/contacts', {
    

    到这里:

    return axios.get('http://127.0.0.1:8000/api/contacts', {
    

    现在它缺少 return 语句,因此返回 undefined

    【讨论】:

    • 给我错误异步操作 › 在获取联系人完成后创建 GET_CONTACTS expect(received).toEqual(expected) 预期值等于:[{"body": { “联系人”:[{“电子邮件”:“shadow@yahoo.com”,“名字”:“影子”,“姓氏”:“madow”,“电话”:5566}]},“类型”:“GET_CONTACTS” }] 收到:[{"payload": [Error: Network Error], "type": "CONTACT_ERROR"}]
    • 好吧,这就是进步! ;) 你原来的问题已经解决了。对于下一个问题,我将尝试在您的 .then((response) => { 函数体中进行一些调试,以查看它是否在错误之前被命中。也可以尝试仔细检查真正的端点没有被击中。
    猜你喜欢
    • 2020-06-11
    • 2020-02-29
    • 1970-01-01
    • 1970-01-01
    • 2018-07-16
    • 2018-05-26
    • 1970-01-01
    • 2020-11-07
    • 2021-06-25
    相关资源
    最近更新 更多