title: How to do a unit test for Http post,put,delete using MockBackend in Angular2?
body: <p>So I am testing my angular2 components &amp; services.</p>
  
  <p>So far I have used mockBackend for mocking get request in service as below:</p>
  
  <pre><code>/* tslint:disable:no-unused-variable */
  
  import { MockBackend } from '@angular/http/testing';
  import { Http, ConnectionBackend, BaseRequestOptions, Response, ResponseOptions } from '@angular/http';
  import { PagesService } from './pages.service';
  import { tick, fakeAsync } from '@angular/core/testing/fake_async';
  import { inject, TestBed } from '@angular/core/testing/test_bed';
  import {GlobalService} from './../../shared/global.service';
  
  describe('PagesService', () =&gt; {
    beforeEach(() =&gt; {
      TestBed.configureTestingModule({
        providers: [
          {
            provide: Http, useFactory: (backend: ConnectionBackend, defaultOptions: BaseRequestOptions) =&gt; {
              return new Http(backend, defaultOptions);
            }, deps: [MockBackend, BaseRequestOptions]
          },
          { provide: PagesService, useClass: PagesService },
          { provide: GlobalService, useClass: GlobalService },
          { provide: MockBackend, useClass: MockBackend },
          { provide: BaseRequestOptions, useClass: BaseRequestOptions }
        ]
      });
    });
  
    //should retrive all search results
    it('should retrieve all search results',
      inject([PagesService, MockBackend], fakeAsync((pagesService: PagesService, mockBackend: MockBackend) =&gt; {
        let res: Response;
        mockBackend.connections.subscribe(c =&gt; {
          expect(c.request.url).toBe('http://localhost:3200/pm/pages/');
          let response = new ResponseOptions({
            body: '[{"name": "Rapid"}, {"name": "NGBC"}]'});
            c.mockRespond(new Response(response));
          });
          pagesService.getAllpages().subscribe((response) =&gt; {
             res = response;              
          });
          tick();
          expect(res[0].name).toBe('Rapid');
       }))
    );
   it('should fetch by Page id',
    inject([PagesService, MockBackend], fakeAsync((pagesService: PagesService, mockBackend: MockBackend) =&gt; {
      let res;
      mockBackend.connections.subscribe(c =&gt; {
        let page_id:string='2';
        expect(c.request.url).toBe('http://localhost:3200/pm/pages/'+page_id);
        let response = new ResponseOptions({body: '[{"id": 1, "name": "Rapid"}, {"id": 2, "name": "NGBC"}]'});
        c.mockRespond(new Response(response));
      });
      pagesService.getPageById('2').subscribe((response) =&gt; {
        res = response;
     });
     tick();
     expect(res[1].name).toBe('NGBC');
     }))
   ); 
  });
  </code></pre>
  
  <p>If you want to see my service.ts its as below:</p>
  
  <pre><code>export class PagesService {
      private headers = new Headers();
      public baseUrl:string;
      constructor(private http: Http,private _globalService:GlobalService) {
          this.baseUrl=this._globalService.baseUrl;
      }
      getAllpages() {
          return this.http.get(this.baseUrl + '/pm/pages/')
              .map((res: Response) =&gt; res.json()).catch(this.handleError);
      }
  
      getPageById(page_id: string) {
          return this.http.get(this.baseUrl + '/pm/pages/' + page_id)
              .map((res: Response) =&gt; res.json()).catch(this.handleError);
      }
  
      savePage(page: Object) {
          this.headers=new Headers();
          this.headers.append('Content-Type', 'application/json');
          let url = this.baseUrl+'/pm/pages/';
          let data={};
          data["data"]=page;
          return this.http.post(url, JSON.stringify(data),{headers: this.headers})
             .map((res: Response) =&gt; res.json()).catch(this.handleError);
      }
  
      updatePage(page: any) {
          this.headers=new Headers();
          this.headers.append('Content-Type', 'application/json');
          let url = this.baseUrl+'/pm/pages/'+page._id;
          let data={};
          data["data"]=page;
          return this.http.put(url, JSON.stringify(data),{headers: this.headers})
              .map((res: Response) =&gt; res).catch(this.handleError);
      }
  
      deletePage(page_id: string) {
           this.headers=new Headers();
           this.headers.append('Content-Type', 'application/json');
           let url = this.baseUrl+'/pm/pages/';
           return this.http.delete(url + page_id,{headers: this.headers,body: ''})
               .map((res: Response) =&gt; res).catch(this.handleError);
      }
  
      mergePage(page: Object) {
          return this.http.post(this.baseUrl + '/pm/pages/',JSON.stringify(page))
              .map((res: Response) =&gt; res.json()).catch(this.handleError);
      }
  
      handleError(error: any) {
          console.error(error);
          return Observable.throw(error.json().error || 'Server error');
      }
  }
  </code></pre>
  
  <p>from the above service, I have implemented getAllPages() &amp; getPageById() methods in my service.spec.ts file correctly as they are working fine.</p>
  
  <p>I want to know how to implement test cases for <code>savePage() , updatePage() , deletePage()</code> methods.</p>
  
  <p>Any inputs?</p>
  
  <p>Thanks in advance.</p>