import { TestBed , inject } from '@angular/core/testing' ; import { HttpClientTestingModule , HttpTestingController } from '@angular/common/http/testing' ; import { HTTP_INTERCEPTORS , HttpClient } from '@angular/common/http' ; import { ErrorHandlerInterceptor } from './error-handler.interceptor' ; describe ( 'ErrorHandlerInterceptor' , ( ) => { let errorHandlerInterceptor ; let http ; let httpMock ; function createInterceptor ( ) { errorHandlerInterceptor = new ErrorHandlerInterceptor ( ) ; return errorHandlerInterceptor ; } beforeEach ( ( ) => { TestBed . configureTestingModule ( { imports : [ HttpClientTestingModule ] , providers : [ { provide : HTTP_INTERCEPTORS , useFactory : createInterceptor , multi : true } ] } ) ; } ) ; beforeEach ( inject ( [ HttpClient , HttpTestingController ] , ( _http , _httpMock ) => { http = _http ; httpMock = _httpMock ; } ) ) ; afterEach ( ( ) => { httpMock . verify ( ) ; } ) ; it ( 'should catch error and call error handler' , ( ) => { spyOn ( ErrorHandlerInterceptor . prototype as any , 'errorHandler' ) . and . callThrough ( ) ; http . get ( '/toto' ) . subscribe ( ( ) => fail ( 'should error' ) , ( ) => { expect ( ErrorHandlerInterceptor . prototype [ 'errorHandler' ] ) . toHaveBeenCalled ( ) ; } ) ; httpMock . expectOne ( { } ) . flush ( null , { status : 404 , statusText : 'error' } ) ; } ) ; } ) ;