【问题标题】:Redux Action Unit Testing (Mocha & Chai)Redux 动作单元测试(Mocha 和 Chai)
【发布时间】:2018-03-22 06:36:39
【问题描述】:

我在为以下 Redux 操作编写单元测试方面需要帮助。 在 Google,我看到了一些示例,但他们编写动作的方式似乎完全不同,因此我在为 Redux 动作编写单元测试时遇到了一些困难。

以下是我拥有的 Redux 操作:

import * as types from './readerTypes';

export const documentSuccessAction = (data) => {
  return (dispatch, getState) => {
    dispatch({
      type: 'GET_METADATA_SUCCESS',
      metadata: data.document
    });

    dispatch({
      type: 'GET_DOCUMENT_SUCCESS',
      document : data.documentContent
    });
  };
};

export const getDocument = (docID) => {
  return (dispatch, getState) => dispatch({
    type: 'API_REQUEST',
    options: {
      method: 'GET',
      service: 'reader',
      endpoint: `document/${docID}`,
      actionTypes: {
        loading: 'GET_DOCUMENT_LOADING',
        success: documentSuccessAction,
        error: 'GET_DOCUMENT_ERROR'
      }
    }
  });
};

谁能帮我解决这个问题?

谢谢

【问题讨论】:

  • 您遇到了什么困难?您尝试了什么,什么不起作用,请详细说明。

标签: reactjs unit-testing redux react-redux redux-thunk


【解决方案1】:

如果我正确理解您正在使用 redux-thunk 库,那么您可以在此处找到一些有关测试 thunk 的有用文档:https://github.com/reactjs/redux/blob/master/docs/recipes/WritingTests.md#async-action-creators

我习惯了这些例子,它对我有用。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-04-11
    • 2018-11-05
    • 1970-01-01
    • 2019-02-03
    • 2018-04-17
    • 2020-12-07
    • 2018-02-27
    相关资源
    最近更新 更多