I have this method that validates the component and it's calling out to this another component which is it's ViewChild
. I am using the component using this.ViewChildComponent.someMethod();
in the component which I am trying to test. I tried to use the spyOn(viewChildComponent, 'someMethod').and.returnValue(true)
.
But it says the this.ViewChildComponent.someMethod() is undefined
. I have all the dependencies such as services for the ViewChildComponent
on the providers. I even went a step forward and made the call that the viewChildComponent
makes to its service to decide someMethod();
Any help would be awesome.
If you have for example
class TestedComponent() {
@ViewChild('ChildComp') public variableChildComponent: ChildComp;
}
In testing spec file declare child component and tested component:
beforeEach(() => {
TestBed.configureTestingModule({
declarations: [ TestedComponent, ChildComp]
})
fixture = TestBed.createComponent(TestedComponent);
comp = fixture.componentInstance;
spyOn(comp.variableChildComponent, 'someMethod').and.returnValue(true);
});
This should work.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With