如何固定预期的$ .length = 2等于1

时间:2019-06-19 15:39:30

标签: angular rxjs jasmine-marbles

我想用茉莉花大理石测试ngLogger函数,但出现错误“预期$ .length = 2等于1”。

期望$ .length = 2等于1。

期望$ [0] .frame = 0等于10。

期望$ [0] .notification.value是一种可观察的对象,但它是Object({type:'TECHNICAL',level:'ERROR',msg:'test'})。

期望$ [1] = Object({框架:0,通知:Notification({种类:'C',值:未定义,错误:未定义,hasValue:false})})等于未定义。

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' });const expected = of({ type: LogEnum.TECHNICAL, level: LevelEnum.ERROR, msg: 'test' }); 我收到错误Expected $[1].frame = 0 to equal 10. 是什么意思?

1 个答案:

答案 0 :(得分:0)

首先有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$ = const result$ = GlobalUtils.ngLogger(msg);

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

此外这里还有working example

相关问题