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

标签 unit-testing rxjs timeout jasmine-marbles

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

Stackblitz

待测管道:

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

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

expect(await source.toPromise()).toEqual({ timeout: true });

用 Jasmine 大理石测试

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

因错误而失败
Expected $.length = 0 to equal 2.
Expected $[0] = undefined to equal Object({ frame: 500, notification: Notification({ kind: 'N', value: Object({ timeout: true }), error: undefined, hasValue: true }) }).
Expected $[1] = undefined to equal Object({ frame: 500, notification: Notification({ kind: 'C', value: undefined, error: undefined, hasValue: false }) }).

最佳答案

最近向 jasmine-marbles 0.5.0 添加了对时间进程的支持 (see jasmine-marbles PR #38)。额外的测试规范被添加到包中,展示了完成你想要的事情的几种可能方法之一。以下是我可以使用您的 Stackblitz 示例拼凑起来的一些选项。

选项1

当您在测试方法之外初始化源 observable 时(例如在 beforeEach 中),您必须显式初始化并将测试调度程序传递给 timeout获取 expect().toBeObservable()在职的。但是,请注意,此更改将破坏“应与 toPromise 一起使用”测试。 (我不知道为什么,但 toPromise() 似乎不适用于这种方法。)

describe('Marble testing with timeout', () => {

  let source;

  beforeEach(() => {
    // You must explicitly init the test scheduler in `beforeEach`.
    initTestScheduler()
    source = cold('a', { a: { id: 'a' } }).pipe(
      // You must explicitly pass the test scheduler.
      timeout(500, getTestScheduler()),
      filter((a) => false),
      catchError(err => {
        return of({ timeout: true })
      }),
      take(1)
    );
  });

  it('should work with toBeObservable', () => {
    const expected = cold('500ms (a|)', { a: { timeout: true } });
    expect(source).toBeObservable(expected);
  });
});

选项 2

您可以稍微重构一下并在测试方法中初始化源可观察对象(不在 beforeEach 中)。您不需要显式初始化测试调度程序(jasmine-marbles 会在测试方法运行之前为您完成),但您仍然需要将其传递给 timeout .请注意 createSource函数可以与测试调度程序或默认调度程序一起使用(如果 scheduler 参数保留 undefined )。此选项适用于“应该与 toPromise 一起工作”测试和“应该与 toBeObservable 一起工作”测试。

describe('Marble testing with timeout', () => {

  const createSource = (scheduler = undefined) => {
    return cold('a', { a: { id: 'a' } }).pipe(
      // You must explicitly pass the test scheduler (or undefined to use the default scheduler).
      timeout(500, scheduler),
      filter((a) => false),
      catchError(err => {
        return of({ timeout: true })
      }),
      take(1)
    );
  };

  it('should work with toPromise', async () => {
    const source = createSource();
    expect(await source.toPromise()).toEqual({ timeout: true });
  });

  it('should work with toBeObservable', () => {
    const source = createSource(getTestScheduler());
    const expected = cold('500ms (a|)', { a: { timeout: true } });
    expect(source).toBeObservable(expected);
  });
});

选项 3

最后,您可以跳过将测试调度程序传递给 timeout如果您明确使用测试调度程序的 run方法,但必须使用 expectObservable (与 expect().toBeObservable() 相反。它工作得很好,但 Jasmine 会报告警告“SPEC HAS NO EXPECATIONS”。

describe('Marble testing with timeout', () => {

  let source;

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

  it('should work with scheduler and expectObservable', () => {
    const scheduler = getTestScheduler();
    scheduler.run(({ expectObservable }) => {
      expectObservable(source).toBe('500ms (0|)', [{ timeout: true }]);
    });
  });
});

关于unit-testing - 如何使用 jasmine-marbles 在 rxjs 管道中测试 timeout(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55819399/

相关文章:

c++ - 我们可以对内存分配进行单元测试吗?

python - 这 3 个 pytest 装置的功能差异是什么?

c# - 对于 BDD/TDD 这种风格的改进有什么建议吗?

unit-testing - 只有在单元测试成功后,Maven 多模块才会部署到存储库

javascript - 为 getScript() 添加超时

c++ - glMapBufferRange 卡住 OpenGL 驱动程序

rxjs - 为什么订阅者会听到主题上的旧错误?

javascript - RxJS:节流限速 API 调用

javascript - RxJS 等待 promise 解决

java - 用于 I/O 超时的最佳异常类型是什么?