![](/img/trans.png)
[英]How to test a ViewChild/ContentChild property in a angular component?
[英]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.