Angular2 unit testing : testing a component's constructor

后端 未结 3 1020
盖世英雄少女心
盖世英雄少女心 2021-02-07 12:20

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

3条回答
  •  醉酒成梦
    2021-02-07 12:52

    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.

提交回复
热议问题