简体   繁体   English

带有nock和redux-mock-store错误的Redux动作测试

[英]Redux action testing with nock and redux-mock-store errors

I am new to redux testing and have been trying to back fill test for an application I made so sorry if this is the complete wrong way to go about testing with nock and 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);
      });
  }
}

This is the method and it seems to be getting called but normally goes to the catch cause of nock failing cause of the header not matching. 这是该方法,似乎正在被调用,但通常转到nock的catch原因,导致报头不匹配的失败原因。

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


    });
  });
});

Even when the header is removed and the nock intercepts the call. 即使已删除标头且nock仍截获了呼叫。 I am getting this error every time 我每次都会收到此错误

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

You're not returning the promise from axios to chain the then call onto. 您并没有从axios返回诺言以链接then调用。

Change the thunk to be like: 将重击更改为:

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

You may also need to change the test so that it doesn't pass before the promise resolves. 您可能还需要更改测试,以便在诺言解决之前不通过测试。 How to do this changes depending on which testing library you use. 更改方式取决于您使用的测试库。 If you're using mocha, take a look at this answer . 如果您使用的是摩卡咖啡,请查看此答案

Side note: I'm not sure if you have other unit tests testing the action creator separately to the reducer, but this is a very integrated way to test these. 旁注:我不确定您是否有其他单元测试分别对动作创建者和reducer进行测试,但这是一种非常集成的测试方法。 One of the big advantages of Redux is how easily each seperate cog of the machine can be tested in isolation to each other. Redux的一大优点是可以轻松地将机器的每个单独的齿轮相互隔离地进行测试。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM