动作创建者中的多个调度员在单元测试中导致问题

时间:2017-05-18 17:31:20

标签: reactjs redux mocha react-redux redux-thunk

以下是我的多次调度的动作创建者

export function getAnalysisById(Id){
  let url = APIEndpoints["getAnalysisById"];
  var dataObject = {
    id: Id
  }
  return dispatch => {
    dispatch ({
                       type:LOADING_ANALYSIS,
                       payload : { isLoading : true}
           });
    const request = axios.post(url, dataObject);
    dispatch ({
                       type:GET_ANALYSIS_BY_ID,
                       payload: request
           });
  };
}

以下是针对上述操作编写的测试代码

var  chai  = require('chai');

var expect = chai.expect;
const actions = require('../../src/actions/index');

import { applyMiddleware } from 'redux'
import thunk from 'redux-thunk'
import nock from 'nock'
import ReduxPromise from 'redux-promise'
const middlewares = [ thunk,ReduxPromise ]


import configureStore from 'redux-mock-store'


const mockStore = configureStore(middlewares)


describe("All actions WITH Asynchronous call",function description(){
  it('should return an action to get All Analysis by id', () => {
  const store = mockStore({})

 // Return the promise
  return store.dispatch(actions.getAnalysisById('costnomics_Actual vs Budjected'))
    .then(() => {
      // const actionss = store.getActions()
       console.log('store',store)

    })
}) 


});

运行上述测试用例会产生以下错误,

TypeError:无法读取属性'然后'未定义的

当我将普通对象作为动作创建者而不是调度程序的返回值传递时,一切正常。但是当多个调度员从动作创建者返回时,它会给我上述错误?为什么会这样?

1 个答案:

答案 0 :(得分:0)

你的thunk函数实际上并没有返回一个promise。你需要在结尾处加上return request之类的东西,以便从thunk返回promise,并从那里返回store.dispatch()