gpt4 book ai didi

angular - 升级到 Angular/rxjs 6 会破坏 Karma 测试

转载 作者:太空狗 更新时间:2023-10-29 17:20:10 26 4
gpt4 key购买 nike

在我升级到新版本的 Angular 之后,我以前的一个测试失败了,我不知道为什么。也就是说,我有一个记录错误的功能:

import { Observable, of } from 'rxjs';

export function handleError<T>(operation='operation', result?: T) {
return (error: any): Observable<T> => {
console.error(error);
console.info(`${operation} failed: ${error.message}`);
return of(result as T);
}
}

我测试它:

it('#handleError return function should return an object', () => {
let errorFunction = handleError('dummyFetch', [{}]);
expect(typeof errorFunction({ message: 'Something went wrong.'})).toEqual('object');
expect(errorFunction({ message: 'Something went wrong.'})).toEqual(of([{}]));
});

失败的行是 expect(errorFunction({ message: 'Something went wrong.'})).toEqual(of([{}])); 并报告错误:预期 $._subscribe = 函数等于函数。。会不会是异步错误函数导致测试失败?

编辑:这是我解决的解决方案:

it('#handleError return function should return an object', () => {
let errorFunction = handleError('dummyFetch', [{}]);
expect(typeof errorFunction({ message: 'Something went wrong.' })).toEqual('object');

let error = errorFunction({ message: 'Something went wrong.' });
error.subscribe(value => {
expect(value).toEqual([{}]);
});
});

最佳答案

如果您将测试重写为

it('#handleError return function should return an object', () => {
let errorFunction = handleError('dummyFetch', [{}]);
expect(typeof errorFunction({ message: 'Something went wrong.'})).toEqual('object');
errorFunction.subscribe((result) => {
expect(result).toEqual([{}]);
});
});

此测试因可观察对象而失败,您最终期望中的订阅应该可以解决此问题。

关于angular - 升级到 Angular/rxjs 6 会破坏 Karma 测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50445225/

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