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?
Declare fixture for child component inside describe.
let childFixture: ComponentFixture<childComponent>;
And now create component instance using
let childComp = childFixture.componentInstance;
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()
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.