Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Test a method which calls another method from the viewChild using Jasmine

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.

like image 772
HarmonicaBlower Avatar asked Dec 30 '16 21:12

HarmonicaBlower


Video Answer


1 Answers

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.

like image 94
Daniel Drozdzel Avatar answered Oct 14 '22 15:10

Daniel Drozdzel