Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to fix Expected $.length = 2 to equal 1

I want to test ngLogger function with jasmine marble but a got error

Expected $.length = 2 to equal 1.

Expected $.length = 2 to equal 1.

Expected $[0].frame = 0 to equal 10.

Expected $[0].notification.value to be a kind of Observable, but was Object({ type: 'TECHNICAL', level: 'ERROR', msg: 'test' }).

Expected $[1] = Object({ frame: 0, notification: Notification({ kind: 'C', value: undefined, error: undefined, hasValue: false }) }) to equal undefined.

Test:

    export namespace GlobalUtils {
       export function ngLogger(error: string): 
                                   Observable<Log> {
        return of({ type: LogEnum.TECHNICAL,
          level: LevelEnum.ERROR,
          msg: error } as Log
        );
      }
    }



    import { GlobalUtils } from './global.utils';

    it('ngLogger should be return an Observable', () => {
        const expected = of({
          type: LogEnum.TECHNICAL,
          level: LevelEnum.ERROR,
          msg: 'test'
        });

        const expected$ = hot('-a', { a: expected });
        const result$ = GlobalUtils.ngLogger('test');

        expect(result$).toBeObservable(expected$);
      });

const expected$ = hot('a', { a: expected }); don't do any difference. const expected$ = hot('a|', { a: expected }); give the error :

    Expected $[0].notification.value to be a kind of Observable, but was Object({ type: 'TECHNICAL', level: 'ERROR', msg: 'test' }).
    Expected $[1].frame = 0 to equal 10

then I changed

const expected = of({
      type: LogEnum.TECHNICAL,
      level: LevelEnum.ERROR,
      msg: 'test'
    });` to `const expected = of({
      type: LogEnum.TECHNICAL,
      level: LevelEnum.ERROR,
      msg: 'test'
    });

I get the error Expected $[1].frame = 0 to equal 10. what does it means ?

like image 686
Ursule Maffo Avatar asked Jun 19 '19 15:06

Ursule Maffo


1 Answers

You have 2 issues first is that marble should be (a|) as this is how you describe simultaneous emit and end observable which is done when using of. The second issue is that you have your expected defined as observable and it should be only the data inside. And thanks to this I have learnt how to use marbles:

const msg = 'test';
const expected = {
  type: LogEnum.TECHNICAL,
  level: LevelEnum.ERROR,
  msg,
}; // notice that this value should not be observable

const expected$ = hot('(a|)', { a: expected }); // also you are returning of which is ending immediately

const result$ = GlobalUtils.ngLogger(msg);

expect(result$).toBeObservable(expected$);

Also in addition here is working example

like image 103
Xesenix Avatar answered Nov 04 '22 20:11

Xesenix