简体   繁体   中英

RxJs - Jasmine marbles forkJoin operator test

Here is forkJoin operator jasmine marble test:

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);
});

The test produces the following errors:

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.

Could anyone tell me what I am doing wrong?

First ---21 will wait three frames, then emit '2' and then emit '1 . Fork join is not a merge it will emit once, and it will emit [1, 2] .

Alphanumeric marbles advance one frame when they emit. So both a and b will complete at frame 4. The forkJoin will then resolve emit. The forkJoin will then immediately complete (also at frame 4).

So the errors you are getting:

Expected $[0].frame = 40 to equal 30. The first item emitted from the fork-join is at time 4 (because a and b complete at time 4) not at time 3.

Expected $[0].notification.value = [ 1, 2 ] to equal '2'. The value emitted is [1, 2] and not '2' as described above.

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.

You are expecting a '1' at time 5. It is getting a complete at time 4.

So you probably want...

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM