【问题标题】:Angular 2.0.0 - Testing " imported by the module 'DynamicTestModule' "Angular 2.0.0 - 测试“由模块'DynamicTestModule'导入”
【发布时间】:2017-02-22 04:57:19
【问题描述】:

我在 Angular 2 中测试 app.component.ts 时遇到问题。我正在使用 angular-cli。每当我运行 ng test 时,我的 app.component.spec.ts 都会在控制台提示错误:

 Failed: Unexpected directive 'HomeModuleComponent' imported by the module 'DynamicTestModule'
 Error: Unexpected directive 'HomeModuleComponent' imported by the module 'DynamicTestModule'

我在 TestBed 中导入了 HomeModuleComponent

TestBed.configureTestingModule({
  declarations: [AppComponent],
  imports : [ HomeModuleComponent ]
});

谁能帮我解决这个问题?

【问题讨论】:

  • 我想我现在已经找到了问题所在。 HomeModuleComponent 必须在声明中而不是在导入中。此外,您可以在您的 TestBed 中导入模块吗?谢谢。

标签: unit-testing angular karma-jasmine


【解决方案1】:

HomeModuleComponent 是 Component 而不是 Module,所以它必须在声明中:

TestBed.configureTestingModule({
  declarations: [AppComponent, HomeModuleComponent],
  imports : [ ]
});

然后您可以创建要测试的组件,

TestBed.createComponent(AppComponent);

【讨论】:

    【解决方案2】:

    在我的测试规范中,我错误地导入了 service 而不是提供它。我遇到了同样的错误。

    在providers 数组中添加service 解决了我的错误。

    【讨论】:

      猜你喜欢
      • 2020-11-23
      • 2017-12-11
      • 2017-06-03
      • 2021-06-19
      • 1970-01-01
      • 2018-04-07
      • 2017-05-07
      • 2022-10-13
      • 2020-12-27
      相关资源
      最近更新 更多