我是 Angular Testing 的新手,我想知道如何测试 ngOnInit 调用时调用的函数。下面是我正在尝试测试的代码
it('should call TestMethod method inside ngOnInit' , async() => {
spyOn(component , 'TestMethod')
fixture.detectChanges()
expect(component.TestMethod).toHaveBeenCalled()
})
下面是我收到的错误
Expected spy TestMethod to have been called.
BeforeEach 位于下方
beforeEach(() => {
fixture = TestBed.createComponent(CreateRoomsComponent);
component = fixture.componentInstance;
fixture.detectChanges();
});
最佳答案
将 spy 对象分配给变量并在expect函数中使用该变量
it('should call TestMethod method inside ngOnInit' , async() => {
const obj = spyOn(component , 'TestMethod')
fixture.detectChanges()
expect(obj).toHaveBeenCalled()
})
关于javascript - 应该调用 ngOnInit 内部的函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60772219/