检查函数是否被调用,Angular 单元测试

raj*_*aju 4 jasmine angular

我在 ngOnInit 中调用了一个函数,

ngOnInit() {
 this.isSubscribable();
}
Run Code Online (Sandbox Code Playgroud)

我想像这样对此 ngOnInit 进行单元测试:

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

    }))
Run Code Online (Sandbox Code Playgroud)

这是行不通的。我需要一些帮助。

Emi*_*ien 6

如果你这样尝试怎么办?

it('Check isSubscribable is called from ngOnInit', () => {
  const spySubscribable = spyOn(component, 'isSubscribable');
  component.ngOnInit();
  expect(spySubscribable).toHaveBeenCalled();
});
Run Code Online (Sandbox Code Playgroud)