【问题标题】:Error: StaticInjectorError(DynamicTestModule)[NGXLogger -> LoggerConfig]错误:StaticInjectorError(DynamicTestModule)[NGXLogger -> LoggerConfig]
【发布时间】:2023-04-01 10:56:01
【问题描述】:

我有以下问题。 当我对项目“A”进行单元测试时,我从 45 个绿色项目中得到 45 个。 现在我添加一个组件“B”。 当我现在开始单元测试时, 我在项目“A”的一个组件上遇到两个错误

错误看起来像这样:

 MainNavComponent should render some nav links   Error:
      StaticInjectorError(DynamicTestModule)[NGXLogger -> LoggerConfig]:   
   StaticInjectorError(Platform: core)[NGXLogger -> LoggerConfig]: 
       NullInjectorError: No provider for LoggerConfig!
    Error: StaticInjectorError(DynamicTestModule)[NGXLogger -> LoggerConfig]: 
      StaticInjectorError(Platform: core)[NGXLogger -> LoggerConfig]:  
    NullInjectorError: No provider for LoggerConfig!

正如我所说,“MainNavComponent”是项目“A”的一部分,在没有新组件“B”的情况下没有错误。

它是怎么来的? 如何解决这个问题?

谢谢, 弗兰克

【问题讨论】:

  • 你在 MainNavComponent 中使用你的组件 B 吗?
  • 是的! (对不起,我忘记注意到了。)
  • 我尝试了很多不同的想法,还在 b.component.ts、b.service.ts、main-nac.component.ts 中实现了 LoggerModule 作为提供者。
  • 请发布您的代码。正如@Orodan 注意到的那样,您已经忘记说明一些非常重要的事情了。
  • @tricheriche 谢谢!

标签: angular unit-testing


【解决方案1】:

只是因为我是新手,需要更具体的答案..

在测试中,更具体的是file.component.spec.ts

beforeEach(async () => {
  await TestBed.configureTestingModule({
    imports: [AppTestingModule],
    declarations: [YourComponent],
    providers: [
      { provide: LoggerConfig, useClass: class {}}
    ]
  }).compileComponents();
});

..希望能帮助大家更快地解决问题。 ;-)

【讨论】:

    【解决方案2】:

    解决方案如错误日志中所写。 我需要在 main-nav.component.ts 中注入 LoggerConfig。

    对不起,这么简单,以前不认识。

    【讨论】:

    • 我已添加到提供者列表中。所以我注入它。
    • 是的,尽管如此,也许您应该编辑您的答案并使用“注入”而不是“实施”。否则可能会令人困惑:)
    • @Orodan 我说。谢谢。
    猜你喜欢
    • 1970-01-01
    • 2018-10-31
    • 2019-06-22
    • 1970-01-01
    • 2021-10-25
    • 2019-07-23
    • 1970-01-01
    • 1970-01-01
    • 2019-02-25
    相关资源
    最近更新 更多