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

时间:2019-03-06 13:10:01

标签: angular rxjs karma-jasmine

希望有人可以对此有所了解,这是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新的throwError。任何帮助将不胜感激!

1 个答案:

答案 0 :(得分:0)

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

throwError应该被测试还是catchError捕获,这取决于您如何设置测试。

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

类似

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