【发布时间】:2020-01-16 13:47:04
【问题描述】:
我正在尝试为以下使用 retryWhen 运算符的函数编写测试:
// some API I'm using and mocking out in test
import { geoApi } from "api/observable";
export default function retryEpic(actions$) {
return actions$.pipe(
filter(action => action === 'A'),
switchMap(action => {
return of(action).pipe(
mergeMap(() => geoApi.ipLocation$()),
map(data => ({ data })),
retryWhen(errors => {
return errors.pipe(take(2));
}),
);
}),
);
}
该代码应该执行对某个远程 API geoApi.ipLocation$() 的请求。如果出现错误,它会在放弃之前重试 2 次。
我编写了以下使用 Jest 和 RxJS TestScheduler 的测试代码:
function basicTestScheduler() {
return new TestScheduler((actual, expected) => {
expect(actual).toEqual(expected);
});
}
const mockApi = jest.fn();
jest.mock('api/observable', () => {
return {
geoApi: {
ipLocation$: (...args) => mockApi(...args),
},
};
});
describe('retryEpic()', () => {
it('retries fetching 2 times before succeeding', () => {
basicTestScheduler().run(({ hot, cold, expectObservable, expectSubscriptions }) => {
const actions$ = hot('-A');
// The first two requests fail, third one succeeds
const stream1 = cold('-#', {}, new Error('Network fail'));
const stream2 = cold('-#', {}, new Error('Network fail'));
const stream3 = cold('-r', { r: 123 });
mockApi.mockImplementationOnce(() => stream1);
mockApi.mockImplementationOnce(() => stream2);
mockApi.mockImplementationOnce(() => stream3);
expectObservable(retryEpic(actions$)).toBe('----S', {
S: { data: 123 },
});
expectSubscriptions(stream1.subscriptions).toBe('-^!');
expectSubscriptions(stream2.subscriptions).toBe('--^!');
expectSubscriptions(stream3.subscriptions).toBe('---^');
});
});
});
此测试失败。
但是,当我将retryWhen(...) 替换为简单的retry(2) 时,测试成功。
看起来我不太明白如何用retryWhen 实现retry。我怀疑这个take(2) 正在关闭流并阻止一切继续进行。但是我不太明白。
我其实想在retryWhen() 里面写一些额外的逻辑,但首先我需要了解如何正确实现retry() 和retryWhen()。或者这实际上是不可能的?
其他资源
我对 retryWhen + take 的实现是基于这个 SO 答案:
官方文档:
【问题讨论】:
标签: unit-testing rxjs take retry-logic