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.
question from:
https://stackoverflow.com/questions/65902243/how-can-i-use-the-angular-appinjector-solution-in-a-karma-test 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…