title: Testing HTTP with mockbackends
body: <p>I'm working on building out tests for my services in Angular 2.  Building out the mock backends is proving to be a real trial.  I have been able to test the services with it making actual HTTP requests successfully but I would like to keep these segregated from the third part.</p>
  
  <p>I've combed through the two main articles I have been able to find (<a href="https://developers.livechatinc.com/blog/testing-angular-2-apps-routeroutlet-and-http/" rel="nofollow">first</a>, <a href="https://medium.com/google-developer-experts/angular-2-unit-testing-with-jasmine-defe20421584#.4ld73ae8c)" rel="nofollow">second</a> as well as the <a href="https://angular.io/docs/ts/latest/guide/webpack.html" rel="nofollow">angular docs</a>.  </p>
  
  <p>When I was making successful http requests to actual third party services, I had to use <code>done();</code> to ensure that the test waited for the request to complete.  It seems that the case is the same here.  If I do not use it, the test will be a success even with <code>expect(1).toBe(2);</code>.  When I do use it, it times out waiting for the call to complete.  Error:</p>
  
  <p><code>Error: Timeout - Async callback was not invoked within timeout specified by jasmine.DEFAULT_TIMEOUT_INTERVAL.</code></p>
  
  <p>Here's my code:</p>
  
  <pre><code>import {
      it,
      inject,
      describe,
      beforeEachProviders,
      expect,
  } from '@angular/core/testing';
  import { BaseRequestOptions, Response, ResponseOptions, Http } from '@angular/http';
  import { MockBackend, MockConnection } from '@angular/http/testing';
  
  import { AccountService } from './../../source/scripts/services/account.service';
  import { provide } from '@angular/core';
  
  describe('AccountService', () =&gt; {
      // let service;
  
      beforeEachProviders(() =&gt; [
          AccountService,
          BaseRequestOptions,
          MockBackend,
          provide(Http, {
              deps: [MockBackend, BaseRequestOptions],
              useFactory: (backend: MockBackend, defaultOptions: BaseRequestOptions) =&gt; {
                  return new Http(backend, defaultOptions);
              },
          }),
      ]);
  
      beforeEach(&lt;any&gt;inject([MockBackend], (backend: MockBackend) =&gt; {
          const baseResponse = new Response(new ResponseOptions({ body: 'got response' }));
          backend.connections.subscribe((c: MockConnection) =&gt; c.mockRespond(baseResponse));
      }));
  
      // We use 'done' for async callbacks (http calls)    
      it('should return mocked response', (done) =&gt; {
  
          inject([AccountService], (testService: AccountService) =&gt; {
              testService.getUsers().subscribe((res: Response) =&gt; {
                  expect(res.text()).toBe('got response');
                  expect(1).toBe(2);
                  done();
              });
          });
      });
  });
  </code></pre>
  
  <p>How do I properly test http calls with a mockbackend?  I suspect that either the links I referenced above are out of date or they didn't confirm that the test was actually testing rather than just giving false positives without the async of <code>done();</code>.</p>