[英]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.