2017-08-09 91 views
3

我有這樣一個組件:Angular2 @Viewchild單元測試

export class ParentComponent implements OnInit, OnDestroy { 


    @ViewChild(ChildComponent) childComponent: ChildComponent; 
} 

它是使用childComponent撥打電話,讓我們這樣說:

this.childComponent.method(); 

ParentComponent方法。

因此,當我試圖測試內部使用ChildComponentParentComponent方法時,childComponent返回時未定義。

如何解決該問題?

回答

0

在大多數情況下,當你想使用ViewChild在您的測試,只是將它添加到您的測試decleration這樣的:

beforeEach(async(() => { 
     TestBed 
      .configureTestingModule({ 
       imports: [], 
       declarations: [ChildComponent], 
       providers: [], 
      }) 
      .compileComponents()