簡體   English   中英

Angular2 @Viewchild單元測試

[英]Angular2 @Viewchild unit test

我有一個像這樣的組件:

export class ParentComponent implements OnInit, OnDestroy {


    @ViewChild(ChildComponent) childComponent: ChildComponent;
}

這是使用childComponent進行調用的,我們這樣說:

this.childComponent.method();

ParentComponent方法中。

因此,當我嘗試測試內部使用ChildComponentParentComponent方法時,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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM