RxJs-茉莉花大理石叉子加入操作员测试

时间:2019-05-27 14:45:07

标签: angular jasmine rxjs jasmine-marbles

这是forkJoin运算符茉莉大理石测试:

it('test1', () => {

 const a = cold('---a|', { a: 1 });
 const b = cold('---b|', { b: 2 });

 const observable = forkJoin(
   a,
   b
 );

 const expected = cold('---21');
 expect(observable).toBeObservable(expected);
});

测试产生以下错误:

Expected $[0].frame = 40 to equal 30.
Expected $[0].notification.value = [ 1, 2 ] to equal '2'.
Expected $[1].frame = 40 to equal 50.
Expected $[1].notification.kind = 'C' to equal 'N'.
Expected $[1].notification.value = undefined to equal '1'.
Expected $[1].notification.hasValue = false to equal true.

有人可以告诉我我在做什么错吗?

1 个答案:

答案 0 :(得分:1)

首先---21将等待三帧,然后发出'2',然后发出'1``. Fork join is not a合并it will emit once, and it will emit [1、2]`。

字母数字大理石在发射时前进一帧。因此ab都将在第4帧处完成。forkJoin随后将解析发射。然后forkForin将立即完成(也在第4帧)。

所以您得到的错误是

Expected $[0].frame = 40 to equal 30.从fork-join发出的第一项是在时间4(因为a和b在时间4完成)而不是在时间3。

Expected $[0].notification.value = [ 1, 2 ] to equal '2'.发出的值为[1, 2],而不是如上所述的'2'

Expected $[1].frame = 40 to equal 50.
Expected $[1].notification.kind = 'C' to equal 'N'.
Expected $[1].notification.value = undefined to equal '1'.
Expected $[1].notification.hasValue = false to equal true.

您期望在时间5到达'1'。在时间4到达complete

所以您可能想要...

const expected = cold('----(a|)', { a: [1, 2] });
expect(observable).toBeObservable(expected);