Jest - 测试报错 TypeError: Cannot read property 'then' of undefined

3
当我运行测试时,它会给我一个错误TypeError: Cannot read property 'then' of undefined。我试图在文档中寻找一些解决方法,但是没有找到可以解决这个问题的方法。如果您有任何想法来解决这个问题,将不胜感激。谢谢。
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个回答

4

请确保从getContacts()函数内部的匿名函数中返回Promise:

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期望值等于: [{"body": {"contacts": [{"email": "shadow@yahoo.com", "first_name": "shadow", "last_name": "madow", "phone": 5566}]}, "type": "GET_CONTACTS"}] 收到的值为: [{"payload": [Error: Network Error], "type": "CONTACT_ERROR"}] expect(received).toEqual(expected) - user-9725874
好的,这样进展还是不错的! ;) 您原来的问题已经解决了。对于下一个问题,我建议您尝试在 .then((response) => { 函数体内进行一些调试,以查看是否在错误之前被触发。也可以尝试双重检查真实的端点是否被触发。 - Will

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接