Angular2 @Viewchild unit test

Viewed 2991

I have a component like this:

export class ParentComponent implements OnInit, OnDestroy {


    @ViewChild(ChildComponent) childComponent: ChildComponent;
}

which is using the childComponent to make a call, let's say like this:

this.childComponent.method();

within ParentComponent method.

So, when I am trying to test the ParentComponent method which is internally using the ChildComponent, the childComponent is returning as undefined.

How to resolve the issue?

2 Answers

Declare fixture for child component inside describe.

let childFixture: ComponentFixture<childComponent>;

And now create component instance using

let childComp = childFixture.componentInstance;
Related