gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 17:06:30 24 4
gpt4 key购买 nike

我想用 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.这是什么意思 ?

最佳答案

您首先有两个问题是大理石应该是 (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

关于angular - 如何将预期的 $.length = 2 修正为等于 1,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56671432/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com