我在史诗中有以下内容:
mergeMap(result => concat(
of(fetchDone(result)),
of(dispatchActions(payload))
))
行动:
const fetchDone = result => ({ type: "FETCH_DONE", payload: result });
function dispatchActions(payload) {
return dispatch => {
dispatch(doStuff(payload));
...
};
}
问题出在我使用弹珠的测试中,我需要能够检查匿名函数,因为dispatchActions
被视为匿名。我怎么做?
const values = {
...
b: { type: "FETCH_DONE", payload: expected },
c: NEEDS TO BE ANONYMOUS
};
...
const output$ = fetchApi(action$, state$);
// This fails due to the anonymous function
expectObservable(output$).toBe('---(bc)--', values);
作为一种解决方法,我正在做:
function dispatchActions(payload) {
if (payload.callDispatches) {
return dispatch => {
dispatch(doStuff(payload));
...
};
}
return { type: "SOME_TYPE" };
}
然后在单元测试中,我只是检查第二次返回。 if-condition测试可以在大理石之外的单独测试中处理。
这不是理想的,但现在解决了这个问题。应该有一些方法来测试使用弹珠的redux-thunk
s。