我正在创建一个用于使用异步操作发出Ajax请求的中间件。中间件拦截原始 Action ,执行ajax请求,并将原始 Action 连同dispatch
的响应一起重新url
编码。
因此,我的组件将仅dispatch
这样的 Action
onClick() {
dispatch(ActionCreator.fetchUser());
}
中间件将保护其余部分,如here所示。
我的问题是,单元测试应该怎么做?我应该模拟
onClick
本身吗?还是应该编写一个模拟的中间件并通过模拟的响应转发操作?我不确定应该采用哪种方法。我尝试了several stuff,但是我尝试的一切对我来说都没有道理。
有指针吗?
最佳答案
注意:以下答案有些过时。
here描述了一种更简单的更新方法。
不过,您仍然可以用其他方式进行操作。
现在,我们在官方文档中添加了a section on testing async action creators。
对于使用Redux Thunk或其他中间件的异步 Action 创建者,最好完全模拟Redux存储以进行测试。您仍然可以将applyMiddleware()
与模拟存储一起使用,如下所示。您还可以使用nock模拟HTTP请求。
function fetchTodosRequest() {
return {
type: ADD_TODOS_REQUEST
};
}
function fetchTodosSuccess(body) {
return {
type: ADD_TODOS_SUCCESS,
body
};
}
function fetchTodosFailure(ex) {
return {
type: ADD_TODOS_FAILURE,
ex
};
}
export function fetchTodos(data) {
return dispatch => {
dispatch(fetchTodosRequest());
return fetch('http://example.com/todos')
.then(res => res.json())
.then(json => dispatch(addTodosSuccess(json.body)))
.catch(ex => dispatch(addTodosFailure(ex)));
};
}
可以像这样测试:
import expect from 'expect';
import { applyMiddleware } from 'redux';
import thunk from 'redux-thunk';
import * as actions from '../../actions/counter';
import * as types from '../../constants/ActionTypes';
import nock from 'nock';
const middlewares = [thunk];
/**
* Creates a mock of Redux store with middleware.
*/
function mockStore(getState, expectedActions, onLastAction) {
if (!Array.isArray(expectedActions)) {
throw new Error('expectedActions should be an array of expected actions.');
}
if (typeof onLastAction !== 'undefined' && typeof onLastAction !== 'function') {
throw new Error('onLastAction should either be undefined or function.');
}
function mockStoreWithoutMiddleware() {
return {
getState() {
return typeof getState === 'function' ?
getState() :
getState;
},
dispatch(action) {
const expectedAction = expectedActions.shift();
expect(action).toEqual(expectedAction);
if (onLastAction && !expectedActions.length) {
onLastAction();
}
return action;
}
}
}
const mockStoreWithMiddleware = applyMiddleware(
...middlewares
)(mockStoreWithoutMiddleware);
return mockStoreWithMiddleware();
}
describe('async actions', () => {
afterEach(() => {
nock.cleanAll();
});
it('creates FETCH_TODO_SUCCESS when fetching todos has been done', (done) => {
nock('http://example.com/')
.get('/todos')
.reply(200, { todos: ['do something'] });
const expectedActions = [
{ type: types.FETCH_TODO_REQUEST },
{ type: types.FETCH_TODO_SUCCESS, body: { todos: ['do something'] } }
]
const store = mockStore({ todos: [] }, expectedActions, done);
store.dispatch(actions.fetchTodos());
});
});