使用redux-saga-test-plan测试redux-saga延迟量

时间:2019-03-08 14:40:06

标签: redux-saga redux-saga-test-plan

背景

我使用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();

1 个答案:

答案 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参数(延迟量)推入数组中然后检查其内容?