简体   繁体   中英

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?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM