Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check if a function has been called, Angular unit testing

I have called a function inside ngOnInit,

ngOnInit() {
 this.isSubscribable();
}

I wan to do unit testing for this ngOnInit like this:

    it('Check isSubscribable is called from ngOnInit', async(async() => {
      spyOn(component, 'isSubscribable').and.callThrough();
      fixture.detectChanges();
      await fixture.whenStable();
      expect(component.isSubscribable).toHaveBeenCalled();

    }))

This is not working. I need some help on this.

like image 682
raju Avatar asked Oct 23 '25 11:10

raju


1 Answers

What if you try like this ?

it('Check isSubscribable is called from ngOnInit', () => {
  const spySubscribable = spyOn(component, 'isSubscribable');
  component.ngOnInit();
  expect(spySubscribable).toHaveBeenCalled();
});
like image 57
Emilien Avatar answered Oct 26 '25 00:10

Emilien



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!