我正在尝试使用 Kent C Dodds 的 @testing-library/angular 来测试角度组件。我的组件依赖于 Angular 服务。我在 api 文档中没有看到有关如何注入模拟服务以为我的单元测试提供模拟数据的信息。
export class ItemCounterComponent implements OnInit {
public availableTodoCount: number;
constructor(private todoProviderService: TodoProviderService) {
this.todoProviderService.getTodos().subscribe(todos => {
this.availableTodoCount = todos.filter(todo => !todo.completed).length;
});
}
ngOnInit() {
}
}
describe('ItemCounterComponent', () => {
it('shows the count correctly', async () => {
const { getByText } = await render(ItemCounterComponent, { componentProperties: { availableTodoCount: 5 } });
expect (getByText('5 items left'));
});
});
````````````