【发布时间】:2020-06-02 19:06:18
【问题描述】:
所以,我在按钮组件上使用了这些方法。
export class SidebarButtonComponent implements OnInit {
private subscribeToRouter(): void {
this.router.events.subscribe(route => {
if (route instanceof NavigationEnd) {
this.isSelected(route.urlAfterRedirects);
}
});
}
private isSelected(route: string): void {
if (this.checkRoute(route)) {
this.selected = true;
} else {
this.selected = false;
}
}
private checkRoute(route: string): boolean {
return route.includes(this.link);
}
}
我知道我无法访问我的 specs 文件中的私有方法,但是 Angular 的代码覆盖率说我没有覆盖它:
59.09% Statements 13/22 37.5% Branches 3/8 42.86% Functions 3/7 52.63% Lines 10/19
测试这些私有测试的最佳方法是什么,或者至少在代码覆盖率中忽略它们?
【问题讨论】:
-
您的组件中应该有一些公共方法调用私有函数,在这种情况下,您可以使用所有可能的组合编写公共方法的 UT,以便它也涵盖私有方法,您将获得私有方法。
标签: angular jasmine karma-runner code-coverage karma-coverage