小编Jod*_*odi的帖子

如何使用 jasmine-marbles 在 rxjs 管道中测试 timeout()

我写了一个过滤输入可观察的管道。在管道中,如果源未及时发出预期值,我使用 timeout() 运算符指定超时以中止等待。我想用 jasmine-marbles 测试超时情况,但我无法让它工作。我相信expect(source).toBeObservable()源发出之前先计算。

堆栈闪电战

待测管道:

source = cold('a', { a: { id: 'a' } }).pipe(
  timeout(500),
  filter((a) => false),
  catchError((err) => {
    return of({ timeout: true })
  }),
  take(1)
);
Run Code Online (Sandbox Code Playgroud)

使用 toPromise() 进行测试按预期工作:

expect(await source.toPromise()).toEqual({ timeout: true });
Run Code Online (Sandbox Code Playgroud)

用茉莉花大理石测试

const expected = cold('500ms (a|)', { a: { timeout: true } });
expect(source).toBeObservable(expected);
Run Code Online (Sandbox Code Playgroud)

因错误而失败

Expected $.length = 0 to equal 2.
Expected $[0] = undefined to equal Object({ frame: 500, notification: Notification({ kind: 'N', …
Run Code Online (Sandbox Code Playgroud)

unit-testing timeout rxjs jasmine-marbles

4
推荐指数
1
解决办法
2233
查看次数

标签 统计

jasmine-marbles ×1

rxjs ×1

timeout ×1

unit-testing ×1