3

Using the top-rated answer for this question:

Storing injector instance for use in components

I have a class with a constructor, which also has service dependencies that are injected as follows:

protected constructor(private settings: MosaicSettings, private imageData: ImageParseData) {
   this.Elements = AppInjector.get(ElementsService);
   ...
}

This works fine for the app:

export class AppModule {
  constructor(injector: Injector) {
    setAppInjector(injector);
  }
}

but within a Karma test, AppInjector is undefined:

TypeError: Cannot read property 'get' of undefined

I've tried everything I can think of to make this work in the test suite, including manually setting TestBed providers and calling setAppInjector() with a newly created Injector. There doesn't seem to be an answer anywhere with regard to testing.

PZL
  • 51
  • 5

0 Answers0