2016-12-30 36 views
3

我有這種驗證組件的方法,它調用另一個組件,它是ViewChild。我正在使用組件中使用this.ViewChildComponent.someMethod();在我試圖測試的組件。我試圖使用spyOn(viewChildComponent, 'someMethod').and.returnValue(true)。 但它說this.ViewChildComponent.someMethod() is undefined。我擁有提供者上的所有依賴項,例如ViewChildComponent的服務。我甚至走了一步,並呼籲viewChildComponent使其服務決定someMethod();測試使用Jasmine從viewChild調用另一個方法的方法

任何幫助將是非常棒的。

+0

要簡明扼要我想知道如何履行扶養ViewChild – HarmonicaBlower

回答

2

,如果你有例如

class TestedComponent() { 
    @ViewChild('ChildComp') public variableChildComponent: ChildComp; 
} 

在測試規範文件declarate子組件和測試組件:

beforeEach(() => { 
    TestBed.configureTestingModule({ 
    declarations: [ TestedComponent, ChildComp] 
    }) 
    fixture = TestBed.createComponent(TestedComponent); 
    comp = fixture.componentInstance; 
    spyOn(TestedComponent.variableChildComponent, 'someMethod').and.returnValue(true); 
}); 

應該努力

相關問題