8

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?

Ankur Mukherjee
  • 3,777
  • 5
  • 32
  • 39

2 Answers2

0

Declare fixture for child component inside describe.

let childFixture: ComponentFixture<childComponent>;

And now create component instance using

let childComp = childFixture.componentInstance;
Tushar Pol
  • 7,119
  • 14
  • 29
  • 36
-2

In most cases when you want to use ViewChild in your test, just add it to the your test decleration like this :

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