当我升级到新版本的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([{}]));
并且报告了错误:Expected $._subscribe = Function to equal Function.
。可能是由于异步错误功能导致测试失败了吗?
编辑:这是我解决的解决方案:
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([{}]);
});
});
由于可观察到的原因,该测试失败了,您的最终期望订阅将解决此问题。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句