Manuela
Manuela

Reputation: 137

How to unit test rxjs interval

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?

Upvotes: 1

Views: 1549

Answers (1)

Jan-Niklas Wortmann
Jan-Niklas Wortmann

Reputation: 421

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

Upvotes: 1

Related Questions