【问题标题】:'Uncaught HttpErrorResponse' Jasmine test failed'未捕获的 HttpErrorResponse' Jasmine 测试失败
【发布时间】:2020-06-24 02:13:32
【问题描述】:

我的服务具有搜索巴西邮政编码的功能。它工作正常。但这不是我的问题。

public getAddress(cepAddress: string): Observable<any> {
        return this.http.get<any>(`${URL + cepAddress}/json/`).pipe(
            map((response: any) => {
                return response;
            }),
            catchError((error: HttpErrorResponse) => {
                return throwError(error);
            })
        );
    }

我一直在尝试测试错误响应,但显然我没有错误。但是当我打开控制台时,我有一个“未捕获的 HttpErrorResponse”错误。这会使我的测试崩溃并且不会产生任何覆盖率,因为这个。我尝试遵循 url 中的角度文档,包括使用 asyncError,但没有成功。错误可以在这里看到

我成功测试了响应案例,但错误案例对我来说有点困难。

const ERRORRESPONSE = new HttpErrorResponse({
  error: 'test 404 error',
  status: 404, statusText: 'Not Found',
  url: 'fakeurl'
});
describe('CadastroService', () => {
  let httpClientSpy;
  let service: CadastroService;

  beforeEach(() => {
    TestBed.configureTestingModule({
      imports: [HttpClientTestingModule]
    });
    httpClientSpy = jasmine.createSpyObj('HttpClient', ['post', 'get']);
    service = new CadastroService(httpClientSpy as any);
    // service = new CadastroService(<any> httpClientSpy);
  });
  it('should test getAddress error', () => {
    httpClientSpy.get.and.returnValue(throwError(ERRORRESPONSE));
    service.getAddress('09890430').subscribe((response) => {
      fail('getAddress error expected');
    }), (error: HttpErrorResponse) => {
      expect(error.status).toEqual(404);
      expect(error.error).toEqual('test 404 error');
    };
  });
});

应用程序版本

"@angular/core": "9.0.6",
"jasmine": "^3.5.0",
"jasmine-core": "~2.99.1",
"jasmine-spec-reporter": "~4.2.1",

我有一个功能非常相似的应用程序,并且测试运行正常,但事实并非如此。 我想让测试通过错误。

谢谢

【问题讨论】:

    标签: angular jasmine karma-jasmine


    【解决方案1】:

    像这样尝试。如果它不起作用,请告诉我。

    beforeEach(() => {
            httpMock = TestBed.get(HttpTestingController);
        service = TestBed.get(CadastroService)
          });
    
        beforeEach(() => {
            TestBed.configureTestingModule({
              imports: [HttpClientTestingModule],
              providers : [CadastroService]
    
            });
        });
    
    
    it('should test getAddress error', () => {
        const ERRORRESPONSE = new HttpErrorResponse({
          error: 'test 404 error',
          status: 404, statusText: 'Not Found',
          url: 'fakeurl'
        });
        service.getAddress('09890430').subscribe((response) => {
          fail('getAddress error expected');
        }, (error: HttpErrorResponse) => {
          expect(error.status).toEqual(404);
          expect(error.error).toEqual('test 404 error');
        });
        const req = httpMock.expectOne(`${URL + cepAddress}/json/`);
        expect(req.request.method).toEqual('GET');
        req.flush(ERRORRESPONSE);
    
        httpMock.verify();
      });
    

    【讨论】:

    • 嗨,伙计。我试过这个,但我必须注释 var cepAddress 才能运行,但即使这样,我也有这个错误 -> NullInjectorError: R3InjectorError(CompilerModule)[HttpTestingController -> HttpTestingController]: NullInjectorError: No provider for HttpTestingController!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-08-06
    • 2020-03-13
    • 1970-01-01
    • 1970-01-01
    • 2021-12-08
    • 1970-01-01
    相关资源
    最近更新 更多