如何修复 Expected $.length = 2 等于 1

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

我想用 jasmine 大理石测试 ngLogger 函数,但出现错误

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.

测试:

    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 }); 没有任何区别。 const expected$ = hot('a|', { a: expected }); 给出错误:

    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

然后我变了

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

我收到错误 Expected $[1].frame = 0 to equal 10. 这是什么意思?

您首先有 2 个问题,大理石应该是 (a|),因为这是您描述使用 of 时完成的同时发出和结束可观察对象的方式。 第二个问题是您将预期定义为可观察的,它应该只是内部的数据。 多亏了这个,我学会了如何使用弹珠:

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

此外这里还有working example