mardi 4 septembre 2018

Angular unit test fails because of component in constructor.

I have a constructor that uses this:

  constructor(
    public core: CoreComponent,
  ) {
   //
  }

But when i try to test these components with a testcase like this:

describe('CoreSubMenuComponent', () => {
  let component: CoreSubMenuComponent;
  let fixture: ComponentFixture<CoreSubMenuComponent>;

  beforeEach(async(() => {
    TestBed.configureTestingModule({
      imports: [
        TestingModule.forRoot(),
      ]
    }).compileComponents();
  }));

  beforeEach(() => {
    fixture = TestBed.createComponent(CoreSubMenuComponent);
    component = fixture.componentInstance;
    fixture.detectChanges();
  });

  it('should create', () => {
    expect(component).toBeTruthy();
  });
});

I get a null injector error:

Error: StaticInjectorError(DynamicTestModule)[CoreSubMenuComponent -> CoreComponent]: 
  StaticInjectorError(Platform: core)[CoreSubMenuComponent -> CoreComponent]: 
    NullInjectorError: No provider for CoreComponent!

It is imported into the testingModule so that isn't the issue. I also tried adding it to the providers array. but that just moves it to the angular renderer which can't be added to the providers array. I also added an @injectable() tag before the @component tag in the CoreComponent but it doesn't change my tests failing.

I tried getting it with @host() but it throws an error. I also tried to get it with the Injector but that also throws an error. These are 4 components that use the CoreComponent as a var to get some info from them but they make the tests fail. All my other tests are pass just fine with over 80 tests. Just these 4 won't work because they try to get properties that are defined in the CoreComponent.

Does someone know how i could reference this component with an instance so that my tests will pass?

Aucun commentaire:

Enregistrer un commentaire