[英]Angular2 @Viewchild unit test
我有一個像這樣的組件:
export class ParentComponent implements OnInit, OnDestroy {
@ViewChild(ChildComponent) childComponent: ChildComponent;
}
這是使用childComponent
進行調用的,我們這樣說:
this.childComponent.method();
在ParentComponent
方法中。
因此,當我嘗試測試內部使用ChildComponent
的ParentComponent
方法時,childComponent返回未定義狀態。
該如何解決?
為內部子組件聲明夾具描述。
let childFixture: ComponentFixture<childComponent>;
現在使用創建組件實例
let childComp = childFixture.componentInstance;
在大多數情況下,當您想在測試中使用ViewChild時,只需將其添加到測試庫中,如下所示:
beforeEach(async(() => {
TestBed
.configureTestingModule({
imports: [],
declarations: [ChildComponent],
providers: [],
})
.compileComponents()
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.