背景
我有一个使用Redux-saga's delay() effect的传奇故事中内置的“渐进式回退”。
我使用来自redux-saga-test-plan的expectSaga来编写测试。
但顺序是to get the test to work I had to disable the delay。
问题
测试延迟量的最佳方法是什么?
代码
返回1、2或3s的"Back off“函数
const BACK_OFF_SERIES = [1000, 2000, 3000];
const backOff = attempt =>
BACK_OFF_SERIES[Math.min(attempt, BACK_OFF_SERIES.length - 1)];tryDownload传奇
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();发布于 2019-03-11 15:47:49
您发布的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参数(延迟量)放入一个数组中,然后检查它的内容吗?
https://stackoverflow.com/questions/55065461
复制相似问题