【问题标题】:Redux action testing with nock and redux-mock-store errors带有 nock 和 redux-mock-store 错误的 Redux 操作测试
【发布时间】:2017-03-08 05:07:36
【问题描述】:

我是 redux 测试的新手,并且一直在尝试对应用程序进行回填测试,如果这是使用 nock 和 redux-mock-store 进行测试的完全错误的方法,我很抱歉。

//Action in authAction.js
export function fetchMessage() {
  return function(dispatch) {
    axios.get(ROOT_URL, {
      headers: { authorization: localStorage.getItem('token') }
    })
      .then(response => {
        console.log("hi")
        dispatch({
          type: FETCH_MESSAGE,
          payload: response.data.message
        });
      })
      .catch(response => {
        console.log(response)
        //callingRefresh(response,"/feature",dispatch);
      });
  }
}

这是方法,它似乎被调用了,但通常会导致标头不匹配的 nock 失败原因的捕获原因。

//authActions_test.js
import nock from 'nock'
import React from 'react'
import {expect} from 'chai'
import configureMockStore from 'redux-mock-store'
import thunk from 'redux-thunk'

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

import * as actions from '../../src/actions/authActions';
const ROOT_URL = 'http://localhost:3090';

describe('actions', () => {

    beforeEach(() => {
        nock.disableNetConnect();
        localStorage.setItem("token", '12345');
    });

    afterEach(() => {
        nock.cleanAll();
        nock.enableNetConnect();
    });


  describe('feature', () => {

    it('has the correct type', () => {
      var scope = nock(ROOT_URL).get('/',{reqheaders: {'authorization': '12345'}}).reply(200,{ message: 'Super secret code is ABC123' });
      const store = mockStore({ message: '' });

      store.dispatch(actions.fetchMessage()).then(() => {
      const actions = store.getStore()
      expect(actions.message).toEqual('Super secret code is ABC123');
    })


    });
  });
});

即使头部被移除并且 nock 拦截了调用。我每次都收到这个错误

TypeError: Cannot read property 'then' of undefined
  at Context.<anonymous> (test/actions/authActions_test.js:43:24)

【问题讨论】:

    标签: unit-testing testing redux redux-thunk redux-mock-store


    【解决方案1】:

    您没有从 axios 返回承诺以链接 then 调用。

    将 thunk 更改为:

    //Action in authAction.js
    export function fetchMessage() {
      return function(dispatch) {
        return axios.get(ROOT_URL, {
          headers: { authorization: localStorage.getItem('token') }
        })
          .then(response => {
            console.log("hi")
            dispatch({
              type: FETCH_MESSAGE,
              payload: response.data.message
            });
          })
          .catch(response => {
            console.log(response)
            //callingRefresh(response,"/feature",dispatch);
          });
      }
    }
    

    您可能还需要更改测试,使其在承诺解决之前不会通过。如何执行此更改取决于您使用的测试库。如果您使用的是 mocha,请查看this answer

    旁注:我不确定您是否有其他单元测试单独测试动作创建器和reducer,但这是测试这些的非常集成的方法。 Redux 的一大优势是可以轻松地对机器的每个单独的齿轮进行相互隔离的测试。

    【讨论】:

    • 这个测试主要是为了确保所有的动作都正确处理响应,并以更正动作的创建来响应。我正计划在另一个文件中测试减速器,以确保它们收到正确的有效负载并在看起来正确后键入状态
    猜你喜欢
    • 2017-08-15
    • 2016-02-29
    • 2021-02-03
    • 2017-01-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-12-18
    相关资源
    最近更新 更多