首页 > 解决方案 > 角度 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。任何帮助将不胜感激!!

标签: angularrxjskarma-jasmine

解决方案


为了从 an 开始或检索值,Observable必须订阅它。

throwError应该被您的测试捕获,或者catchError取决于您如何设置测试。

但是,这应该使您朝着正确的方向前进。

所以像

//...brevity
service.handleError(response)
.pipe(
catchError(result => expect(result).toEqual(...mockError)))
.subscribe();

推荐阅读