简体   繁体   English

为什么Redux Promise Middleware不会为我的示例代码发送被拒绝的操作?

[英]Why does Redux Promise Middleware not dispatch a rejected action for my example code?

I use fetch-mock, redux-mock-store, promise-middleware to test the redux implementation of my application. 我使用fetch-mock,redux-mock-store,promise-middleware测试我的应用程序的redux实现。 I have following code: 我有以下代码:

import configureMockStore from 'redux-mock-store';
import promiseMiddleware from 'redux-promise-middleware';
import fetchMock from 'fetch-mock';
import thunk from 'redux-thunk';
import createLogger from 'redux-logger';
import { bindActionCreators } from 'redux';
import { ACTION_1, hostNameSearchActions }
    from '../../../src/actions/hostNameSearchActions';

const middlewares = [thunk, promiseMiddleware(), createLogger()];
let mockStore = configureMockStore(middlewares);
const SERVICE_URL = 'http://url_to_the_service';
describe('Testing thunk actions', () => {
let store = mockStore({ hostData: { key1 :'value'} });
const aHostNameSearch = bindActionCreators({ ...hostNameSearchActions }, store.dispatch).hostNameSearch;
afterEach(() => {
        fetchMock.reset();
        fetchMock.restore();
        mockStore = configureMockStore(middlewares);
        store = mockStore({ hostData: { key1 :'value'} });
    });
it('ACTION_1_PENDING, ACTION_1_REJECTED dispatched, payload matches expected payload', (done) => {
    fetchMock
        .mock(`${SERVICE_URL}`,
            404 );

    const expectedActions = [
        { type: `${ACTION_1}_PENDING` },
        { type: `${ACTION_1}_REJECTED`, payload: {error: 'test.body.error.message'}}
    ];
    aHostNameSearch().then(() => {
        expect(store.getActions()).toEqual(expectedActions);
        done();
    });

});
});

The problem is that 404 call I am mocking with retchMock always ends up being resolved as ACTION_1_FULFILLED. 问题是我正在使用retchMock嘲笑的404调用始终最终以ACTION_1_FULFILLED形式被解析。 why would this be the case? 为什么会这样呢? Am I mocking the call incorrectly? 我打错电话了吗?

Redux Promise Middleware always dispatches a rejected action when given a rejected action. 当给定拒绝操作时,Redux Promise中间件始终会调度拒绝操作。 If your mocked action always ends up being a fulfilled action, when you expect a rejected action, it is because the promise payload is fulfilled. 如果您嘲笑的操作始终以完成的操作结束,那么当您期望被拒绝的操作时,这是因为已实现了承诺有效载荷。

This can happen if you have any side-effects (eg, any functions that use the then method on the promise) and don't properly pass the error up to the middleware. 如果您有任何副作用(例如,在promise上使用then方法的任何函数)并且没有正确将错误传递给中间件, then可能发生这种情况。 Without more context, though, it's impossible to give you a definitive answer. 但是,如果没有更多的背景信息,就不可能给出确切的答案。 It would be helpful if you included your hostNameSearchActions . 如果您包含hostNameSearchActions这将很有帮助。

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

相关问题 Redux Promise中间件如何反应将结果操作发送到调度? - How does react redux promise middleware send the resulting action to the dispatch? 为什么我的redux定制中间件需要在操作时返回调度? - why my redux custom middleware need to return dispatch on action? 为什么 dispatch 返回一个没有结果的承诺? (redux 中间件) - Why is dispatch returning a promise with no result? (redux middleware) 如果我的操作创建者返回 promise,Redux 何时解决调度? - When does Redux resolve a dispatch if my action creator return promise? 当承诺在 Redux 中间件中解决时,为什么我不能分派操作? - Why can't I dispatch an action when a promise resolves inside Redux middleware? 在Redux中间件中调度多个动作 - Dispatch multiple action in redux middleware 如何在Redux中间件中调度动作? - how to dispatch a action in redux middleware? 为什么每个Redux中间件都可以调用next(action),而不是多次调度一个action? - Why every Redux Middleware can call next(action), wouldn't it dispatch one action multiple times? 如何在Redux中从诺言中调度动作? - How to dispatch an action from a promise in redux? 为什么我在 Redux 中的调度 function 得到一个 function 而不是一个动作? - Why my dispatch function in Redux gets a function instead of an action?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM