【发布时间】: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)
})
我已经验证waitLength 和recordTimeLength 是相同的,因为它们都需要作为测试的上一步的一部分,无论如何这都是有效的。 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