开玩笑 - 测试给出错误 TypeError: Cannot read property 'then' of undefined

Posted

技术标签:

【中文标题】开玩笑 - 测试给出错误 TypeError: Cannot read property \'then\' of undefined【英文标题】: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);
        );
      );
    );

【问题讨论】:

【参考方案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) => 函数体中进行一些调试,以查看它是否在错误之前被命中。也可以尝试仔细检查真正的端点没有被击中。

以上是关于开玩笑 - 测试给出错误 TypeError: Cannot read property 'then' of undefined的主要内容,如果未能解决你的问题,请参考以下文章

TypeError:state.customerComplaints.add 不是开玩笑单元测试中的函数

用 TypeScript 开玩笑:TypeError: environment.teardown 不是函数

开玩笑测试 - TypeError:document.createRange 不是函数

反应开玩笑测试错误 - 未定义 useContext 的对象

Vue.Js 测试与开玩笑错误 axios

TypeError:无法使用玩笑读取未定义的属性“原型”