Angular 2单元测试:无法读取未定义的属性“root”

前端之家收集整理的这篇文章主要介绍了Angular 2单元测试:无法读取未定义的属性“root”前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我的模板包含

<a [routerLink]="['/my-profile']" routerLinkActive="active">
    <i class="icon-user"></i>{{ 'MY_PROFILE' | translate }}
</a>

错误是因为routerLink和routerLinkActive指令.

单元测试文件.

TestBed.configureTestingModule({
    imports: [
        MultilingualModule,CommonModule,RouterTestingModule.withRoutes([
            { path: 'my-profile',component: MockComponent },{ path: 'change-password',])
    ],schemas: [ NO_ERRORS_SCHEMA ],declarations: [TopNavigationBarComponent,TestComponent,MockComponent],providers: [
        { provide: LoginService,useValue: MockLoginService },{ provide: Router,useClass: RouterStub }
    ]
});

解决方法

问题是你实际上是在覆盖路由器来进行测试,这使得它破坏了routerLink.
你可以在这里看到如何对测试路线进行单元测试的一个很好的解释: Angular 2 Final Release Router Unit Test
@H_404_28@

猜你在找的Angularjs相关文章