小编Ben*_*sis的帖子

使用 @testing-library/Angular 注入模拟服务

我正在尝试使用 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'));
  });
});
````````````
Run Code Online (Sandbox Code Playgroud)

angular angular-testing-library

3
推荐指数
1
解决办法
1883
查看次数

标签 统计

angular ×1

angular-testing-library ×1