angular 7 rxjs升级失败的单元测试

问题描述 投票:1回答:1

希望有人可以对此有所了解,对angular和rxjs不熟悉。从Angular 5升级到7,rxjs 5升级到6后,我得到了一些失败的单元测试。

util.service.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。任何帮助将不胜感激!!

angular rxjs karma-jasmine
1个回答
0
投票

要从Observable开始或检索值,您必须订阅它。

throwError应根据您的测试方式,通过您的测试或catchError捕获。

这应该会让你朝着正确的方向前进。

所以像

//...brevity
service.handleError(response)
.pipe(
catchError(result => expect(result).toEqual(...mockError)))
.subscribe();
© www.soinside.com 2019 - 2024. All rights reserved.