微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

使用 rxjs 进行弹珠测试时如何暂停和恢复虚拟时间?

如何解决使用 rxjs 进行弹珠测试时如何暂停和恢复虚拟时间?

我正在尝试为同时具有反应性元素和非反应性元素的对象编写测试。我不知道如何写大理石图才能让测试清晰易读。

在下面的测试中,我有一个我正在测试的对象,它既存储一个值又将它发布到一个主题。我想编写测试,以便我可以发出一些值、停止虚拟时间并检查一些断言。然后,我想恢复虚拟时间。所以,我想我想使用 flush

下面的测试成功,但不清晰:source2expected2 的弹珠图没有相互对齐,所以我无法真正“看到”测试是写得正确。如果我在对 source2 的第一次调用上方定义 expectObservable,那么对 expectObservable 的第二次调用永远不会看到任何值。

class StoresLatestValue {
  constructor() {
    this.subject = new Subject();
  }
  emit(value) {
    this.latest = value;
    this.subject.next(value);
  }
}

test("StoresLatestValue works",() => {
  new TestScheduler((actual,expected) => expect(actual).toEqual(expected)).run(
    (helpers) => {
      const { flush,hot,expectObservable } = helpers;
      const obj = new StoresLatestValue();

      // First half of the test
      const source1 = hot("a-b-c");
      const expected1 = "  a-b-c";
      source1.subscribe((val) => obj.emit(val));
      expectObservable(obj.subject).toBe(expected1);
      flush();

      // Check that the latest value is what we expect
      expect(obj.latest).toBe("c");

      // These next two marble diagrams work,but since they don't line up,// it's hard to kNow that the test is written correctly
      const source2 = hot("d-e--");
      const expected2 = "  ----d-e--";
      source2.subscribe((val) => obj.emit(val));
      expectObservable(obj.subject).toBe(expected2);
      flush();

      // Check that the latest value is what we expect
      expect(obj.latest).toBe("e");
    }
  );
});

我已尝试将订阅运算符 ^ 添加source,但似乎没有帮助。我也尝试使用冷 observable,但我仍然无法写出弹珠,所以一切都很好地排列:

// Making the second observable cold works,but still doesn't line up
const source1 = hot(" a-b-c");
const expected1 = "   a-b-c";
const source2 = cold("  -d-e--");
const expected2 = "   -----d-e--";

// Making them both cold doesn't seem to work at all,nothing is observed
// by the second expectObservable
const source1 = cold("a-b-c");
const expected1 = "   a-b-c";
const source2 = cold("-----d-e--");
const expected2 = "   -----d-e--";

有没有办法编写这个测试,使它看起来正确?

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。