title: How to unit test an HTTP interceptor in Angular using Jasmine
body: <p>I have the below http interceptor in my angular application and I would like to unit test the same using Jasmine. I googled some of them and tried but its not working as expected. Please find the below HttpInterceptorService.ts file code</p>
  <pre><code>export class HttpInterceptorService Implements HttpInterceptor {
   counter = 0;
   constructor(private loaderService: LoaderService) { }
   intercept(req: HttpRequest&lt;any&gt;, next: HttpHandler) {
    if (req.url !== '/getUsers') {
     this.counter ++;
    }
    this.loaderService.setStatus(true);
    return next.handle(req).pipe(
     finalize(() =&gt; {
      if (req.url !== 'getUsers') {
        this.counter --;
      }
      if (this.counter === 0) {
        this.loaderService.setStatus(false);
      }
     };
    );
   }
  }
  </code></pre>
  <p>Below are the HttpInterceptor.service.spec.ts file code which I tried as of now. Im not sure how to test the particular method in it.</p>
  <pre><code>describe('HttpInterceptorService', () =&gt; {
    let httpService: HttpService;
    let httpMock: HttpTestingController;
    let interceptor: HttpInterceptorService;
  
    beforeEach(()=&gt; {
      TestBed.configureTestingModule({
        imports: [HttpClientTestingModule],
        providers: [
         HttpService,
         {provide:HTTP_INTERCEPTOR, useClass: HttpInterceptorService, multi: true},
        ]
      });
      httpService = TestBed.get(HttpService);
      httpMock = TestBed.get(HttpTestingController);
      interceptor = TestBed.get(HttpInterceptorService);
    });
  
     it('should increment the counter for all api's expect getUsers', ()=&gt; {
        httpService.get('getAdminList').subscribe(res =&gt; {
          expect(res).toBeTruthy();
          expect(interceptor.counter).toBeGreaterThan(0);
        });
     });
  
     
  });
  </code></pre>
  <p>after checking the reference code I'm able to cover few lines of code with above changes. But I'm still not able to cover the finalize method. Request to kindly help.</p>