问题描述
我正在尝试为同时具有反应性元素和非反应性元素的对象编写测试。我不知道如何写大理石图才能让测试清晰易读。
在下面的测试中,我有一个我正在测试的对象,它既存储一个值又将它发布到一个主题。我想编写测试,以便我可以发出一些值、停止虚拟时间并检查一些断言。然后,我想恢复虚拟时间。所以,我想我想使用 flush
。
下面的测试成功,但不清晰:source2
和 expected2
的弹珠图没有相互对齐,所以我无法真正“看到”测试是写得正确。如果我在对 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 (将#修改为@)