假设我有以下异步操作创建者:
export const loadUserToken = () => async (dispatch, getState) => {
dispatch({ type: 'LOAD_USER_TOKEN_START' });
try {
const token = await AsyncStorage.getItem('token');
dispatch({ type: 'LOAD_USER_TOKEN_SUCCESS', payload: token });
} catch (error) {
dispatch({ type: 'LOAD_USER_TOKEN_ERROR', payload: error });
}
dispatch(doSomethingHere());
};
export const doSomethingHere = () => async (dispatch, getState) => {
dispatch({ type: 'SOME_TASK_START' });
// ...some logic
dispatch({ type: 'SOME_TASK_END' });
}
对此代码我有两个问题:
我进行了以下测试(使用sinon和redux-mock-store),并且在loadUserToken内模拟了dispatch(doSomethingHere())
。
const error = new Error();
sinon.stub(AsyncStorage, 'getItem').rejects(error);
const expectedActions = [
{ type: Actions.LOAD_USER_TOKEN_START },
{ type: Actions.LOAD_USER_TOKEN_ERROR, payload: error }
];
const store = mockStore(initialState);
return store.dispatch(UserActions.loadUserToken()).then(() => {
expect(store.getActions()).to.eql(expectedActions);
AsyncStorage.getItem.restore();
});
上面的测试也执行了doSomethingHere异步动作创建者,但我不想模拟此方法的整个逻辑(有时可能很长很复杂),我只需要检查此方法是否已执行。