【问题标题】:React Native testing a race redux-saga effect not workingReact Native 测试种族 redux-saga 效果不起作用
【发布时间】:2019-08-05 15:55:57
【问题描述】:

我正在尝试测试我的一个 saga 函数,其中有一个 race。 我的代码和我的测试比赛看起来几乎相同,但比赛测试似乎失败了

  // in my saga
  yield race({
    delay: delay(waitLength),
    cancel: take(smartlook.actions.cancelDelay)
  });

  // part of the test
  .race({
    delay: delay(recordTimeLength),
    cancel: take(smartlookActions.cancelDelay)
  })

我已经验证waitLengthrecordTimeLength 是相同的,因为它们都需要作为测试的上一步的一部分,无论如何这都是有效的。 take 动作指向完全相同的动作,只是由于名称冲突,在导入中的名称略有不同。

当我运行我的测试时,我得到了

SagaTestError:
race expectation unmet:

Expected
--------
{ delay:
   { _40: 0,
     _65: 0,
     _55: null,
     _72: null,
     '@@redux-saga/CANCEL_PROMISE': [Function] },
  cancel:
   { '@@redux-saga/IO': true,
     TAKE: { pattern: { [Function: actionCreator] toString: [Function] } } } }

Actual:
------
1. { delay:
   { _40: 1,
     _65: 0,
     _55: null,
     _72:
      { onFulfilled: { [Function: currCb] cancel: [Object] },
        onRejected: [Function],
        promise: { _40: 0, _65: 0, _55: null, _72: null } },
     '@@redux-saga/CANCEL_PROMISE': [Function] },
  cancel:
   { '@@redux-saga/IO': true,
     TAKE: { pattern: { [Function: actionCreator] toString: [Function] } } } }

我不知道为什么测试中的delay 与真实代码不同,如果有人知道这应该如何工作,请告诉我。

为下面的上下文粘贴更多代码

在传奇中:

export function* recordThenWait(waitLength) {
  const isRecording = yield select(smartlook.selectors.getSmartlookIsRecording);
  if (!isRecording) {
    yield put(smartlook.actions.resumeRecording());
  }
  yield race({
    delay: delay(waitLength),
    cancel: take(smartlook.actions.cancelDelay)
  });
  yield put(smartlook.actions.pauseRecording());
}

in test(测试的不仅仅是上面的函数):

  return expectSaga(Smartlook, {recordTimeLength, now})
    .withState(initialState)
    .dispatch(appStarted())
    .put(smartlookActions.recordAppStart())
    .fork(smartlook.recordThenWait, recordTimeLength)
    .put(smartlookActions.resumeRecording()) //works till here
    .race({
      delay: delay(recordTimeLength),
      cancel: take(smartlookActions.cancelDelay)
    })
    .put(smartlookActions.pauseRecording())
    .run();

【问题讨论】:

    标签: javascript react-native jestjs


    【解决方案1】:

    所以问题是 delay 本身不是一个效果,并且比赛需要一个效果才能正常工作,所以我只是将比赛更改为在延迟上使用 call 而不是在实际和测试中

    yield race({
      delay: call(delay, waitLength),
      cancel: take(smartlook.actions.cancelDelay)
    });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-09-17
      • 1970-01-01
      • 2018-09-03
      • 1970-01-01
      • 2018-12-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多