Angular 5 testing: how to get a reference to the child component

前端 未结 3 1765
隐瞒了意图╮
隐瞒了意图╮ 2020-12-24 12:06

I\'m trying to test the interaction between a host component and a child component in an Angular application. I don\'t know how to get a reference to the child component c

3条回答
  •  醉酒成梦
    2020-12-24 12:56

    Iterate through the childNodes of the debugElement(s) and access the context property to get access to the component ant its properties

    let debugElement = hostFixture.debugElement.childNodes[x] as DebugElement
    debugElement = debugElement.childNodes[x] as DebugElement
    ...
    let component = debugElement.context as YourComponent
    

    This is a very static approach because if a new child is added then maybe you access the wrong childNode. It is better to write a helper method which traverse through the childNodes and find the right name.

提交回复
热议问题