0

I have subsriber to an observable on ngOnInit and now I am trying to write test for this which doesn't seems working.

Here is my class which I am trying to test

ngOnInit() {
  this.myService.updates$.pipe(takeUntil(unsusbribe$))
   .subscribe(update => {
     this.feeds = update;
  })
}

Here is my unit test for this

it('should set feeds when an update is sent', fakeAsync(() => {
const update = 'fakeUpdate';
component.ngOnInit();
  const serviceSpy = spyOn(myService, 'updates$').and.returnValue(timer(500).pipe(mapTo(update)))
tick(500);
expect(serviceSpy).toHaveBeenCalled(); 
}))

This test fails with error Expected spy updates$ to have been called.

Can anyone please help me figuring out the issue here.

undefined
  • 3,464
  • 11
  • 48
  • 90

1 Answers1

0

Correct way to use spy service. Please try this one.

  it('should set feeds when an update is sent', fakeAsync(() => {
    const update = 'fakeUpdate';
    const serviceSpy = spyOn(myService, 'updates$');
    serviceSpy.and.returnValue(timer(500).pipe(mapTo(update)))
    tick(500);
    expect(serviceSpy).toHaveBeenCalled(); 
  }))
Mohit Saxena
  • 1,439
  • 1
  • 12
  • 21