All is in the title : how can one test what is done in the component\'s constructor ?
For your information, I am using a service that requires a setting
The simple way to test anything inside constructor function is to create component instance and then test it.
it('should call initializer function in constructor', () => {
TestBed.createComponent(HomeComponent); // this is the trigger of constructor method
expect(sideNavService.initialize).toHaveBeenCalled(); // sample jasmine spy based test case
});
One thing to note that, if you want to distinguish between constructor and ngOnInit, then don't call fixture.detectChanges()
inside beforeEach()
. instead call manually whenever you need.