可能是因为在测试中没有等待异步操作完成。可以使用fakeAsync或async来解决这个问题。
fakeAsync
async
示例代码:
it('should call the function', fakeAsync(() => { spyOn(component, 'myFunction'); component.myAsyncFunction(); tick(); //等待异步调用完成 expect(component.myFunction).toHaveBeenCalled(); }));
上一篇:Angular测试:覆盖使用模板而不是模板URL
下一篇:Angular测试:尽管函数明显被调用,但测试失败。