unit-testing - 带有 nock 和 redux-mock-store 错误的 Redux Action 测试

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

我是 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);
      });
  }
}

这是方法,它似乎正在被调用,但通常会转到捕获头不匹配的诺克失败原因的原因。

//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');
    })


    });
  });
});

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

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

最佳答案

您没有返回 axios 的 promise 以链接 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);
      });
  }
}

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

旁注:我不确定您是否有其他单元测试分别测试 action creator 和 reducer,但这是一种非常集成的测试方法。 Redux 的一大优势是可以轻松地相互隔离地测试机器的每个独立齿轮。

关于unit-testing - 带有 nock 和 redux-mock-store 错误的 Redux Action 测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42663416/

相关文章:

java - 哪个是最好的 Java 隔离框架? JMock、Easymock、Mockito 或其他?

c++ - 验证输出到 "Find the numbers between 1 and 1000 whose prime factors' 总和本身就是一个素数”来自 Allain 的 Jumping into C++ (ch7 #3)

ruby-on-rails - rails : How to test delegated methods?

javascript - 处理标准化响应的最佳方法是什么?

javascript - 在 useEffect 中使用 reducer 状态

python - 具有原始规范 Python 的模拟类

php - Selenium 浏览器窗口大小

Python unittest 返回 "Ran 0 tests in 0.000s",我不知道为什么

git - merge Hook ,用于在没有 merge 请求的情况下通过所有测试

reactjs - 分派(dispatch)操作是否总是会导致应用程序状态发生变化?