angular - 角度 7 rxjs 升级失败的单元测试
问题描述
希望有人能对此有所了解,对 angular 和 rxjs 不熟悉。从 Angular 5 升级到 7 和 rxjs 5 到 6 后,我得到了一些失败的单元测试已经通过。
实用服务.ts
handleError(error: HttpErrorResponse | any) {
let errors: any = [];
if (error instanceof HttpErrorResponse) {
if (error.status) {
switch (error.status) {
case 400:
errors = error.error.errors || '';
break;
case 401:
errors = [{ status: error.status, message: 'Please log in.' }];
return Observable;
case 403:
errors = [{ status: error.status, message: 'Access denied.' }];
break;
case 404:
errors = [{ status: error.status, message: 'The requested application is not found.' }];
break;
case 500:
errors = [
{ status: error.status, message: 'Sorry, we were unable to process your request. Please try again.' }
];
break;
default:
errors = [{ status: error.status }];
}
return throwError(errors);
}
}
try {
errors = error.error.errors;
return throwError(errors);
} catch (err) {
return throwError([
{ status: 500, message: 'Sorry, we were unable to process your request. Please try again.' }
]);
}
}
util.service.spec.ts
it('should throw an observable if response has status 404', () => {
const response = new HttpErrorResponse({ status: 404, error: { errors: ['error'] } });
expect(service.handleError(response)).toEqual(
throwError([{ status: 404, message: 'The requested application is not found.' }])
);
});
业力错误
Error: Expected $._subscribe = Function to equal Function.
at stack (http://localhost:9876/absolute/Users/mtlaney/Desktop/coding_monkey/fs-open-forest-platform/frontend/node_modules/jasmine-core/lib/jasmine-core/jasmine.js?0b1eaf7a13cae32191eadea482cfc96ae41fc22b:2455:17)
at buildExpectationResult (http://localhost:9876/absolute/Users/mtlaney/Desktop/coding_monkey/fs-open-forest-platform/frontend/node_modules/jasmine-core/lib/jasmine-core/jasmine.js?0b1eaf7a13cae32191eadea482cfc96ae41fc22b:2425:14)
at Spec.expectationResultFactory (http://localhost:9876/absolute/Users/mtlaney/Desktop/coding_monkey/fs-open-forest-platform/frontend/node_modules/jasmine-core/lib/jasmine-core/jasmine.js?0b1eaf7a13cae32191eadea482cfc96ae41fc22b:901:18)
at Spec.addExpectationResult (http://localhost:9876/absolute/Users/mtlaney/Desktop/coding_monkey/fs-open-forest-platform/frontend/node_modules/jasmine-core/lib/jasmine-core/jasmine.js?0b1eaf7a13cae32191eadea482cfc96ae41fc22b:524:34)
at Expectation.addExpectationResult (http://localhost:9876/absolute/Users/mtlaney/Desktop/coding_monkey/fs-open-forest-platform/frontend/node_modules/jasmine-core/lib/jasmine-core/jasmine.js?0b1eaf7a13cae32191eadea482cfc96ae41fc22b:845:21)
at Expectation.toEqual (http://localhost:9876/absolute/Users/mtlaney/Desktop/coding_monkey/fs-open-forest-platform/frontend/node_modules/jasmine-core/lib/jasmine-core/jasmine.js?0b1eaf7a13cae32191eadea482cfc96ae41fc22b:2369:12)
at UserContext.it (http://localhost:9876/_karma_webpack_/webpack:/src/app/_services/util.service.spec.ts:59:43)
at ZoneDelegate../node_modules/zone.js/dist/zone.js.ZoneDelegate.invoke (http://localhost:9876/_karma_webpack_/webpack:/node_modules/zone.js/dist/zone.js:391:1)
at ProxyZoneSpec.push../node_modules/zone.js/dist/proxy.js.ProxyZoneSpec.onInvoke (http://localhost:9876/_karma_webpack_/webpack:/node_modules/zone.js/dist/proxy.js:129:1)
at ZoneDelegate../node_modules/zone.js/dist/zone.js.ZoneDelegate.invoke (http://localhost:9876/_karma_webpack_/webpack:/node_modules/zone.js/dist/zone.js:390:1)
我已将所有内容从 Observable.throw 更改为 rxjs new throwError。任何帮助将不胜感激!!
解决方案
为了从 an 开始或检索值,Observable
您必须订阅它。
throwError
应该被您的测试捕获,或者catchError
取决于您如何设置测试。
但是,这应该使您朝着正确的方向前进。
所以像
//...brevity
service.handleError(response)
.pipe(
catchError(result => expect(result).toEqual(...mockError)))
.subscribe();
推荐阅读
- excel - 将字符串数组传递给 Excel VBA 中的 Range().Formula
- javascript - json-server - 奇怪的自动增量 ID
- javascript - 创建新对象并将其推送到反应状态数组中
- sftp - IBM Sterling 文件传输
- c# - 将函数作为参数传递给另一个函数
- ios - 在导航栏上放置栏按钮时出现问题
- c++ - C++十进制转二进制,如何转换?
- android - 如何将样式添加到 .mbtiles 文件中
- rest - DialogFlow Fulfillment 连接到 REST API
- azure - Azure CLI 从表中选择一个选项