我使用Redux-saga's delay()
效果将“渐进补偿”内置到传奇中。
我正在使用redux-saga-test-plan中的expectSaga
编写测试。
但顺序为to get the test to work I had to disable the delay。
测试延迟量的最佳方法是什么?
“回退”功能返回1s,2s或3s
const BACK_OFF_SERIES = [1000, 2000, 3000];
const backOff = attempt =>
BACK_OFF_SERIES[Math.min(attempt, BACK_OFF_SERIES.length - 1)];
尝试下载传奇
const MAX_ATTEMPTS = 3;
function* tryDownloadFiles(assets, downloadOptions) {
for (let attempts = MAX_ATTEMPTS; attempts; attempts -= 1) {
try {
const urls = yield call(downloader, assets, downloadOptions);
return urls;
} catch (error) {
if (attempts > 1) {
yield call(delay, backOff(attempts));
} else {
yield put(
showError({
error: `${error}. Failed!`,
})
);
}
}
}
}
使用提供者的expectSaga
测试绕过delay
:
const res = expectSaga(tryDownloadFiles, mockAssets, mockDownloadOptions)
.provide([
[matchers.call.fn(delay), null],
[matchers.call.fn(downloader), throwError(mockError)],
])
// .call(delay, backOff(3))
// .call(delay, backOff(2))
// .call(delay, backOff(1))
.put(showError({ error: `${mockError}. Failed!` }))
.run();
答案 0 :(得分:0)
您发布的Dynamic Providers docs报告了此示例
.provide({
call(effect, next) {
// Check for the API call to return fake value
if (effect.fn === api.fetchUser) {
const id = effect.args[0];
return { id, name: 'John Doe' };
}
// Allow Redux Saga to handle other `call` effects
return next();
},
那么,您是否不能将所有effect.args
参数(延迟量)推入数组中然后检查其内容?