[英]Injecting mock services with @testing-library/angular
I am trying to test an angular component using @testing-library/angular by Kent C Dodds.我正在尝试使用 Kent C Dodds 的 @testing-library/angular 测试角度组件。 My component has a dependency on an Angular service.
我的组件依赖于 Angular 服务。 I see no information in the api docs on how to inject a mock service to provide mock data for my unit test.
我在 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'));
});
});
````````````
You can use providers
to provide a service (just like you can do with TestBed)您可以使用
providers
来提供服务(就像您可以使用 TestBed 一样)
const component = await render(AppComponent, {
providers: [
{
provide: TodoProviderService,
useValue: { getTodos: ... },
},
],
});
More examples at: https://github.com/testing-library/angular-testing-library/tree/master/src/app/examples更多示例请访问: https : //github.com/testing-library/angular-testing-library/tree/master/src/app/examples
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.