如何将预期的 $.length = 2 修正为等于 1

Urs*_*ffo 8 rxjs angular jasmine-marbles

我想用茉莉花大理石测试 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.
Run Code Online (Sandbox Code Playgroud)

测试:

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.
Run Code Online (Sandbox Code Playgroud)

const expected$ = hot('a', { a: expected });不要做任何区别。const expected$ = hot('a|', { a: expected });给出错误:

    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$);
      });
Run Code Online (Sandbox Code Playgroud)

然后我改变了

    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
Run Code Online (Sandbox Code Playgroud)

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

Xes*_*nix 9

您首先有两个问题是,大理石应该是这样的,(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$);
Run Code Online (Sandbox Code Playgroud)

另外这里是工作示例