title: Angular2 Jasmine spyOn http calls
body: <p>I would like to spy on the <code>Http</code> service to see if it calls the right endpoint. Is it possible to do that ? </p>
  
  <p>So far, in my services, all I'm testing is that they send back data ... I would like to make them more useful. </p>
  
  <p>If you ever need it, here is my current spec file : </p>
  
  <pre><code>import { TestBed, async, inject } from '@angular/core/testing';
  import { SignupService } from './signup.service';
  import { Observable } from 'rxjs/Observable';
  
  import { HttpModule, Http, Response, ResponseOptions, RequestOptions, Headers, XHRBackend } from '@angular/http';
  import { MockBackend, MockConnection } from '@angular/http/testing';
  import { ErrorHandlerService } from '../../services/error-handler.service';
  
  describe('SignupService', () =&gt; {
  
    let errorStub = {};
  
    beforeEach(() =&gt; {
      TestBed.configureTestingModule({
        imports: [HttpModule],
        providers: [
          SignupService,
          { provide: XHRBackend, useClass: MockBackend },
          { provide: ErrorHandlerService, useValue: errorStub }
        ]
      });
    });
  
    it('signup should return an application error if no apps are provided', inject([SignupService], (service: SignupService) =&gt; {
      service.signup('', [], null).subscribe(
        suc =&gt; { },
        err =&gt; expect(err).toMatch(/application/)
      );
    }));
  
    it('signup should return a role error if no roles are provided', inject([SignupService], (service: SignupService) =&gt; {
      service.signup('', null, []).subscribe(
        suc =&gt; { },
        err =&gt; expect(err).toMatch(/rôle/)
      );
    }));
  
    it(
      'signup should return a response with a "contenu" parameter',
      inject([SignupService, XHRBackend], (service: SignupService, mockBackEnd: MockBackend) =&gt; {
        let content = 'anything';
        mockBackEnd.connections.subscribe((connection: MockConnection) =&gt; {
          connection.mockRespond(new Response(new ResponseOptions({
            body: JSON.stringify({
              contenu: content
            })
          })));
        });
        service.signup('', [], []).subscribe(
          suc =&gt; expect(suc).toBe(content)
        );
      }));
  
    it(
      'checkUser should return a response with a "contenu" parameter',
      inject([SignupService, XHRBackend], (service: SignupService, mockBackEnd: MockBackend) =&gt; {
        let content = 'anything';
        mockBackEnd.connections.subscribe((connection: MockConnection) =&gt; {
          connection.mockRespond(new Response(new ResponseOptions({
            body: JSON.stringify({
              contenu: content
            })
          })));
        });
        service.checkUser('').subscribe(
          suc =&gt; expect(suc).toBe(content)
        );
      }));
  });
  </code></pre>