可能的原因是测试没有正确引用被测组件或服务中的依赖项。此外,测试可能会忽略异步操作或延迟加载的组件和服务。
以下是一个可能的
在测试用例中,使用 spyOn() 函数来模拟依赖项的函数调用。例如:
let mockService = { someFunction() { // Implementation of the mock function } };
// Create a spy on the mockService's someFunction spyOn(mockService, 'someFunction');
// Inject the mock service into the component or service being tested ... providers: [ { provide: RealService, useValue: mockService }, ... ]
// Call a function on the component or service that should trigger the someFunction call component.someOtherFunction();
// Expect the someFunction to have been called expect(mockService.someFunction).toHaveBeenCalled();
注意:如果被测试组件或服务在 ngOnInit 生命周期钩子之后调用依赖项中的函数,请在测试用例中使用 fixture.detectChanges() 以确保组件或服务已经初始化。