【问题标题】:How do I check for anonymous functions?如何检查匿名函数?
【发布时间】:2019-08-19 05:32:04
【问题描述】:

我在史诗中有以下内容:

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】:

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

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

    然后在单元测试中,我只检查第二次返回。并且 if 条件测试可以在弹珠之外的单独测试中处理。

    这并不理想,但现在可以解决问题。不过应该有一些方法可以使用弹珠测试redux-thunks。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-07-06
      • 2011-01-11
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多