Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.0k views
in Technique[技术] by (71.8m points)

dependency injection - How can I use the Angular AppInjector solution in a Karma test?

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...