如何使用 rxjs 弹珠测试发出分组事件的可观察对象?

How to test observables which emit grouped events with rxjs marbles?

根据rxjs marbles documentation,同步分组的当前行为如下:

'(ab)-(cd)': on frame 0, emits a and b then on frame 50, emits c and d

来自文档:

While it can be unintuitive at first, after all the values have synchronously emitted time will progress a number of frames equal to the number of ASCII characters in the group, including the parentheses

好的,但是我如何测试这样的可观察对象(使用弹珠或任何其他技术):

const observable$ = of(1, 2).concat(of(3, 4).delay(20));

有什么解决方法吗?

Stack Overflow 上有 a similar question 但 'How to actually work around it and test this kind of observable' 上没有答案。

谢谢!

我不知道您使用的是哪个版本的 RxJS,因为您混合使用了原型运算符和 pipable 运算符,但它看起来像 RxJS 5.5。

在 RxJS 中 5.X 它有点笨拙。您可以像这样重写您的测试:

import { of } from 'rxjs/observable/of';
import { TestScheduler } from 'rxjs/testing/TestScheduler';
import { assert } from 'chai';
import 'rxjs/add/operator/concat';
import 'rxjs/add/operator/delay';

const scheduler = new TestScheduler((actual, expected) => {
  console.log(actual, expected);
  return assert.deepEqual(actual, expected);
});

const observable$ = of('a', 'b').concat(of('c', 'd').delay(50, scheduler));

scheduler
  .expectObservable(observable$)
  .toBe('(ab)-(cd|)');

scheduler.flush();

查看现场演示(打开控制台):https://stackblitz.com/edit/rxjs5-marble-test?file=index.ts

你知道这个测试通过了,因为它没有抛出任何错误。尝试更改 next 发射的任何延迟或值,它会抛出错误。

也看看这个答案:

但是,我强烈建议升级到 RxJS 6,因为它使 coldhot "creation" 函数的一切变得更容易,您可以只使用 const observable$ = cold('(ab)-(cd|)')创建与 of(...).concat(...).

相同的序列

在 RxJS 6 中测试:

对于我的项目,我迁移到了 rx-sanbox,其中同步分组工作正常,它解决了我的问题。

因此,在 rx-sandbox 中这是正确的: '(ab)-(cd)': on frame 0, emits a and b then on frame 20, emits c and d