1

I'm trying to write a unit test with 100% code coverage (line) and I'm stuck with the following code snippet:

getValuesPeriodically(updateInterval: number) {
    interval(updateInterval)
      .subscribe(() =>
        this.getFilesFromService()
    );
  }

I don't know how to cover this part:

() =>
  this.getFilesFromService()

I tried implementing a unit test with fakeAsync and tick(), but the async gave me an error message:

it('timer test', fakeAsync(() => {

      fixture.detectChanges();
      expect(component.filesData.length).toBe(0);
      tick(1000);
      fixture.detectChanges();
      expect(component.filesData.length).toBeGreaterThan(0);

    }));

I get the following error:

TypeError: Cannot read property 'assertPresent' of null

I'm not sure if this is the right way to cover the missing part of my unit test anyways.

Can you help please?

Manuela
  • 127
  • 1
  • 15
  • In general when testing async Observables you need to pass an instance of `TestScheduler` to `interval` to avoid using real delays. https://github.com/ReactiveX/rxjs/blob/master/docs_app/content/guide/testing/marble-testing.md – martin Jul 31 '19 at 10:34

1 Answers1

1

In general your code looks good to me. Your approach is super valid, I do it that way on my own. So I think it's more like a problem with your general setup. Maybe this can help you Testing | Cannot read property 'assertPresent' of undefined at resetFakeAsyncZone