【问题标题】:Mocha/Chai testing in react/ redux: Use custom middleware for async actionsreact/redux 中的 Mocha/Chai 测试:使用自定义中间件进行异步操作
【发布时间】:2016-09-29 11:42:27
【问题描述】:

我最初是在做 mocha/chai 测试。在我尝试使用反应模拟之前很好,然后 mocha/chai 似乎停止工作。现在我什至看不到有多少测试通过或失败。

对于我的一些测试用例,他们不断返回以下内容:

Error: Actions must be plain objects. Use custom middleware for async actions.

【问题讨论】:

  • 你能附上你的错误测试用例代码吗?

标签: reactjs mocha.js redux react-redux chai


【解决方案1】:

您应该将 thunk 中间件应用于您的 redux 存储。 以下是我在测试期间的做法:

 import thunk from 'redux-thunk';
 import configureStore from 'redux-mock-store';

 describe('Your test', () => {
   const mockStore = configureStore([thunk]);

   it('success case', () => {
     const store = mockStore({});

     return store.dispatch(yourAsyncFunction()).then(() => {
       const action = store.getActions()[0];

       expect(action.type).to.equal(EXPECTED_ACTION);
     });
   });
 });

【讨论】:

    猜你喜欢
    • 2016-12-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-03-12
    • 1970-01-01
    • 2018-11-12
    相关资源
    最近更新 更多