如何在 Karma 测试中使用 Angular AppInjector 解决方案?

问题描述

使用评分最高的答案回答这个问题:

Storing injector instance for use in components

我有一个带有构造函数的类,它也有如下注入的服务依赖项:

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

这适用于应用程序:

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

但在 Karma 测试中,AppInjector 未定义:

TypeError: Cannot read property 'get' of undefined

我已经尝试了所有我能想到的方法来在测试套件中完成这项工作,包括手动设置 Testbed 提供程序和使用新创建的 Injector 调用 setAppInjector()。似乎没有任何关于测试的答案。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)