如何检查匿名功能?

问题描述 投票:0回答:1

我在史诗中有以下内容:

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);
redux-observable rxjs-marbles
1个回答
0
投票

作为一种解决方法,我正在做:

function dispatchActions(payload) {
  if (payload.callDispatches) {
    return dispatch => {
      dispatch(doStuff(payload));
      ...
    };
  }
  return { type: "SOME_TYPE" };
}

然后在单元测试中,我只是检查第二次返回。 if-condition测试可以在大理石之外的单独测试中处理。

这不是理想的,但现在解决了这个问题。应该有一些方法来测试使用弹珠的redux-thunks。

© www.soinside.com 2019 - 2024. All rights reserved.