我有一个我正在尝试测试的动作创建器:
export const fetchAllItems = (topicIds)=>{
return (dispatch)=>{
topicIds.forEach((topicId, index, array)=>{
const last = index+1 == array.length;
dispatch(fetchItems(topicId, last));
});
};
};我要声明的是,fetchItems已经被调用了两次--第一次是使用1, false,第二次是使用2, true。我尝试过redux-mock-store,但不确定是否使用正确:
import configureStore from 'redux-mock-store';
import thunk from 'redux-thunk';
const middlewares = [thunk];
const mockStore = configureStore(middlewares);
const store = mockStore();
store.dispatch(fetchAllItems([1, 2])).then(()=>{
console.log(store.getActions()); //Throws error: TypeError: Cannot read property 'then' of undefined
});我也试过模拟dispatch和fetchItems,但似乎也不能正常工作。
发布于 2017-02-26 19:20:11
我通常只使用spy来捕获分派的操作,而不是使用模拟存储
let dispatch = sinon.spy()
fetchAllItems([1, 2, 3])(dispatch)
expect(dispatch).to.have.been.calledWithMatch({ type: "...", ... })如果thunk不直接分派动作,这确实会变得更加困难,但您可以使用thunk获取thunk并重复此过程,直到发送标准动作为止。
let dispatch = sinon.spy()
fetchAllItems([1, 2, 3])(dispatch)
let fetchItems = dispatch.getCall(0).args[0]
fetchItems(dispatch)
expect(dispatch).to.have.been.calledWithMatch({ type: "...", ... })发布于 2017-02-23 11:49:35
代码的问题在于调用.then,但动作创建者不返回Promise (除非需要,否则不必返回)。您可以将测试更改为:
store.dispatch(fetchAllItems([1, 2]));
console.log(store.getActions());https://stackoverflow.com/questions/42404664
复制相似问题