title: Angular2 - HTTP call unit testing
body: <p>I am a beginner of angular2.  </p>
  
  <p>My service.ts will be,</p>
  
  <pre><code>import { Injectable } from '@angular/core';
  import { Http, Response } from '@angular/http';
  import {Headers, RequestOptions, URLSearchParams}  from '@angular/http';
  import {Observable} from 'rxjs/Observable';
  import 'rxjs/add/operator/map';
  import 'rxjs/add/operator/catch';
  import 'rxjs/add/observable/throw';
  
  @Injectable()
  export class HomeService {
  
      url : string;
  
      constructor(private _http: Http) {}
  
      getUserInfo(userId : string) {
  
          this.url = "http://localhost:9080/GetUserData";
  
          let content = new URLSearchParams();
          content.set('userId', userId.toString());
  
          let headers = new Headers();
          headers.append('Content-Type', 'application/x-www-form-urlencoded');
  
          return this._http
              .post(this.url, content.toString(), {headers: headers})
              .map((res: Response) =&gt; res.json())
              .catch(this.handleError);
      }
  
      handleError (error: Response | any) {
  
          let errMsg: string;
          if (error instanceof Response) {
              const body = error.json() || '';
              const err = body.error || JSON.stringify(body);
              errMsg = `${error.status} - ${error.statusText || ''} ${err}`;
          } else {
              errMsg = error.message ? error.message : error.toString();
          }
          console.error(errMsg);
          return Observable.throw(errMsg);
      }
  
  }
  </code></pre>
  
  <p>I have written the unit test case as,</p>
  
  <pre><code>import { TestBed, async, inject } from '@angular/core/testing';
  import { HomeService } from './home.service';
  import { BaseRequestOptions, Response, ResponseOptions, Http } from '@angular/http';
  import { MockBackend, MockConnection } from '@angular/http/testing';
  import {Observable} from 'rxjs/Observable';
  
  describe('FacilityPopupService', () =&gt; {
    let subject: HomeService = null;
    let backend: MockBackend = null;
    let userid="UID1";
  
    beforeEach(() =&gt; {
      TestBed.configureTestingModule({
          providers: [
            HomeService,
            Response,
          BaseRequestOptions,
          MockBackend,
          {
            provide: Http,
            useFactory: (backend: MockBackend, defaultOptions: BaseRequestOptions) =&gt; {
              return new Http(backend, defaultOptions);
            },
            deps: [MockBackend, BaseRequestOptions],
          }
  
            ]
      });
    });
  
    beforeEach(inject([HomeService, MockBackend], (userService: HomeService, mockBackend: MockBackend) =&gt; {
      subject = userService;
      backend = mockBackend;
  
    }));
  
    it('get Facility list should call endpoint and return it\'s result', (done) =&gt; {
        backend.connections.subscribe((connection: MockConnection) =&gt; {
            let options = new ResponseOptions({
              body: JSON.stringify({ success: true })
            });
            connection.mockRespond(new Response(options));
        });
  
        subject.getUserInfo(userid)
          .subscribe((response) =&gt; {
              expect(response).toEqual({ success: true });
              done();
        });
  
    });
  
  });
  </code></pre>
  
  <p>I want to write the test case for both success case and error case. I don't know how how to execute <strong>catch</strong> block. Also I don't know how to write test case for  <strong>handleError</strong> method . </p>
  
  <p>I cannot cannot cover all the possible scenarios here. Can you please help me to get 100% coverage for the service.ts.</p>