2016-12-30 33 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); 
}); 

应该努力

相关问题