Angular: 如何在 angular 单元测试中使用路由器
Angular: How to use router in angular unit testing
我正在 angular 中编写测试用例。我写了一个条件if res.length === 1
redirect to the details page。 (this.router.navigate(['details', id])
)。我从主体响应中对象的第一个数组中获取 id
作为 const id = res[0].id
。这两行都没有包含在我的代码覆盖范围内。谁能告诉我哪里出错了?
我得到 Expected spy navigate to have been called with [ [ '/product-details', 'SAAASD0001' ] ] but it was never called.
app.component.spec.ts
let router = {navigate: jasmine.createSpy('navigate')};
TestBed.configureTestingModule({
imports: [RouterTestingModule],
providers: [
{ provide: Router, useValue: router }
]
})
it('should take data from store', () => {
const mockData = [
{
id: '123',
name: 'Stackoverlow',
}
]
expect(component.getList).toEqual(mockData);
const productId = mockData[0].id;
expect(router.navigate).toHaveBeenCalledWith(['/details', id]);
});
app.component.ts
getList() {
this.store
.select('content', 'catalogue')
.pipe(takeUntil(this.onDestroy$))
.subscribe((res) => {
Iif (res.length === 1) {
// this line doesn't cover
const id = res[0].id;
// this line doesn't cover
this.router.navigate(['details', id]);
} else {
this.list = category(res);
}
});
}
在组件中制作 Router
和 Store
public 并尝试:
TestBed.configureTestingModule({
imports: [RouterTestingModule],
providers: [Router,Store ]
})
it('should take data from store', () => {
const response = [{id: 'val'}];
spyOn(component.store,"select").and.returnValue(of(response));
spyOn(component.router,"navigate").and.callThrough();
component.getList();
expect(router.navigate).toHaveBeenCalledWith(['/details', response[0].id]);
});
同样通过更改 const response = [{id: 'val'}];
覆盖 else
部分
将这个东西添加到您的导入中:
RouterTestingModule.withRoutes([
{ path: 'path1', component: TestComponent1},
{ path: 'home', component: DashboardComponent }
]),
稍后在您的测试用例中:您可以监视路由器的 "Navigate",并使用它重定向到导入中提到的任何组件。
spyOn(Router,"navigate").and.callthrough();
router.navigate([/path1]);
我正在 angular 中编写测试用例。我写了一个条件if res.length === 1
redirect to the details page。 (this.router.navigate(['details', id])
)。我从主体响应中对象的第一个数组中获取 id
作为 const id = res[0].id
。这两行都没有包含在我的代码覆盖范围内。谁能告诉我哪里出错了?
我得到 Expected spy navigate to have been called with [ [ '/product-details', 'SAAASD0001' ] ] but it was never called.
app.component.spec.ts
let router = {navigate: jasmine.createSpy('navigate')};
TestBed.configureTestingModule({
imports: [RouterTestingModule],
providers: [
{ provide: Router, useValue: router }
]
})
it('should take data from store', () => {
const mockData = [
{
id: '123',
name: 'Stackoverlow',
}
]
expect(component.getList).toEqual(mockData);
const productId = mockData[0].id;
expect(router.navigate).toHaveBeenCalledWith(['/details', id]);
});
app.component.ts
getList() {
this.store
.select('content', 'catalogue')
.pipe(takeUntil(this.onDestroy$))
.subscribe((res) => {
Iif (res.length === 1) {
// this line doesn't cover
const id = res[0].id;
// this line doesn't cover
this.router.navigate(['details', id]);
} else {
this.list = category(res);
}
});
}
在组件中制作 Router
和 Store
public 并尝试:
TestBed.configureTestingModule({
imports: [RouterTestingModule],
providers: [Router,Store ]
})
it('should take data from store', () => {
const response = [{id: 'val'}];
spyOn(component.store,"select").and.returnValue(of(response));
spyOn(component.router,"navigate").and.callThrough();
component.getList();
expect(router.navigate).toHaveBeenCalledWith(['/details', response[0].id]);
});
同样通过更改 const response = [{id: 'val'}];
else
部分
将这个东西添加到您的导入中:
RouterTestingModule.withRoutes([
{ path: 'path1', component: TestComponent1},
{ path: 'home', component: DashboardComponent }
]),
稍后在您的测试用例中:您可以监视路由器的 "Navigate",并使用它重定向到导入中提到的任何组件。
spyOn(Router,"navigate").and.callthrough();
router.navigate([/path1]);