【发布时间】:2021-06-28 12:26:17
【问题描述】:
假设我订阅了组件中的路由参数:
this.route.params.subscribe((params) => {
// what the params object holds
// params.id1 params.id2
// what the current route looks like
//localhost/params.id1/params.id2
});
如何在 Angular 中对 params.id2 进行单元测试?示例:我想测试 params.id2 > 0
目前我已经这样做了:
// top of the describe
let route: ActivatedRoute;
//inside the TestBed.configureTestingModule
providers: [
{
provide: ActivatedRoute,
useValue: {
params: of({
id1: 1,
id2: 0,
}),
},
},
],
route = TestBed.inject(ActivatedRoute);
it('shouldn't be zero', () => {
// i want to check if params.id2 is not zero
expect(params.id2).not.toBe(0);
});
我没有任何使用单元测试的经验。我是否必须像在组件中那样订阅 route.params,或者我如何实现测试方法?
【问题讨论】:
标签: angular typescript unit-testing jasmine karma-jasmine