Angular 角度单元测试-如何在覆盖容器中获取子组件
我有一个名为Angular 角度单元测试-如何在覆盖容器中获取子组件,angular,angular-material2,angular-test,Angular,Angular Material2,Angular Test,我有一个名为MatSelectControls的自定义组件,它的使用方式如下: <component-im-testing> <mat-select> <mat-select-controls></mat-select-controls> <mat-option *ngFor="..."></mat-option> </mat-select> </component-im
MatSelectControls
的自定义组件,它的使用方式如下:
<component-im-testing>
<mat-select>
<mat-select-controls></mat-select-controls>
<mat-option *ngFor="..."></mat-option>
</mat-select>
</component-im-testing>
但我真正想要的是MatSelectControls
组件。尝试以相同的方式选择它不起作用:
const matControlsRef = fixture.debugElement.query(By.directive(MatSelectControls)); //returns null
我假设这不起作用,因为MatSelectControls
实际上在MatSelect
创建的cdk覆盖层内部进行渲染
如何检索
MatSelectControls
组件实例,即使它实际上在cdk覆盖层内呈现?如果没有完整的代码,很难提供帮助,但您可以做的只是通过父组件中的@ViewChild()
添加对mat select controls
组件的引用:
@ViewChild(MatSelectControls) matSelectControls: MatSelectControls;
然后在测试中使用它:
component.matSelectControls.abc
我在这里遇到的问题是:我如何知道matSelectControls定义的时刻像这样
fixture.detectChanges();fixture.whenStable()然后(()=>{/*这里编码*/}
。
component.matSelectControls.abc