繁体   English   中英

使用viewchild和指令进行角度组件测试

[英]Angular component test with viewchild and directive

我因问题出了问题... https://embed.plnkr.co/UparfJDomOECGJfWSbqA/简短:我无法测试我的组件,在container.component.spec.ts:43似乎失败了,但是没有ngAfterViewInit fixture.detectChanges()ngAfterViewInit不会启动。 我看不到我在做什么错。

我想使用指令属性将组件动态加载到ng-template中,并且该解决方案确实有效,但是我无法为其编写有效的测试。 既然我对单元测试还很陌生...那是我什至应该测试的东西-我认为是。 (是的,我阅读了angular.io测试指南以及其他一些文章;-))错误是TypeError: Cannot read property 'viewContainerRef' of undefined这似乎表明@ViewChild没有ViewChild ...

测试模块看不到MenuDirective因为您忘记了从假模块中导出它:

@NgModule({
    declarations: [
        MenuComponent,
        MenuDirective,
    ],
    exports: [MenuDirective], <== add this
    entryComponents: [MenuComponent]
})
class FakeEntryComponents {}

似乎您要查询MenuComponent

menuElement = fixture.debugElement.query(By.directive(MenuComponent))

柱塞示例

也可以看看

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM