title: Unit Test RxJS Observable.timer using typescript, karma and jasmine
body: <p>Hi I'm relatively new to Angular2, Karma and Jasmine. Currently I'm using Angular 2 RC4 Jasmine 2.4.x 
  I have an Angular 2 service which periodically calls an http service like this:</p>
  
  <pre class="lang-typescript prettyprint-override"><code>getDataFromDb() { return Observable.timer(0, 2000).flatMap(() =&gt; {
          return this.http.get(this.backendUrl)
              .map(this.extractData)
              .catch(this.handleError);
      });
  }
  </code></pre>
  
  <p>Now I want to test the functionality. For testing purposes I have just tested the "http.get" on a separate function without the Observable.timer by doing:</p>
  
  <pre class="lang-typescript prettyprint-override"><code>const mockHttpProvider = {
      deps: [MockBackend, BaseRequestOptions],
      useFactory: (backend: MockBackend, defaultOptions: BaseRequestOptions) =&gt; {
          return new Http(backend, defaultOptions);
      }
  }
  
  describe('data.service test suite', () =&gt; {
      var dataFromDbExpected: any;
  
      beforeEachProviders(() =&gt; {
          return [
              DataService,
              MockBackend,
              BaseRequestOptions,
              provide(Http, mockHttpProvider),
          ];
      });
  
      it('http call to obtain data',
          inject(
              [DataService, MockBackend],
              fakeAsync((service: DataService, backend: MockBackend) =&gt; {
                  backend.connections.subscribe((connection: MockConnection) =&gt; {
                      dataFromDbExpected =  'myData';
                      let mockResponseBody: any = 'myData';
                      let response = new ResponseOptions({ body: mockResponseBody });
                      connection.mockRespond(new Response(response));
  
                  });
                  const parsedData$ = service.getDataFromDb()
                      .subscribe(response =&gt; {
                          console.log(response);
                          expect(response).toEqual(dataFromDbExpected);
                      });
              })));
  });
  </code></pre>
  
  <p>I obviously want to test the whole function with the Observable.timer. I think one might want to use the TestScheduler from the rxjs framework, but how can I tell to only repeat the timer function for x times? I couln't find any documentation using it in the typescript context.</p>
  
  <p>Edit: I'm using rxjs 5 beta 6</p>
  
  <p>Edit: Added working example for Angular 2.0.0 final release:</p>
  
  <pre class="lang-typescript prettyprint-override"><code>describe('when getData', () =&gt; {
      let backend: MockBackend;
      let service: MyService;
      let fakeData: MyData[];
      let response: Response;
      let scheduler: TestScheduler;
  
      beforeEach(inject([Http, XHRBackend], (http: Http, be: MockBackend) =&gt; {
          backend = be;
          service = new MyService(http);
          fakeData = [{myfake: 'data'}];
          let options = new ResponseOptions({ status: 200, body: fakeData });
          response = new Response(options);
  
          scheduler = new TestScheduler((a, b) =&gt; expect(a).toEqual(b));
          const originalTimer = Observable.timer;
          spyOn(Observable, 'timer').and.callFake(function (initialDelay, dueTime) {
              return originalTimer.call(this, initialDelay, dueTime, scheduler);
          });
      }));
      it('Should do myTest', async(inject([], () =&gt; {
          backend.connections.subscribe((c: MockConnection) =&gt; c.mockRespond(response));
          scheduler.schedule(() =&gt; {
              service.getMyData().subscribe(
                  myData =&gt; {
                      expect(myData.length).toBe(3,
                          'should have expected ...');
                  });
          }, 2000, null);
          scheduler.flush();
      })));
  });
  </code></pre>