Unit testing 角度2.0.0-测试“;由模块导入';动态测试模块'&引用;

Unit testing 角度2.0.0-测试“;由模块导入';动态测试模块'&引用;,unit-testing,angular,karma-jasmine,Unit Testing,Angular,Karma Jasmine,我在Angular 2中测试app.component.ts时遇到问题。我正在使用angular cli。无论何时运行ng测试,我的app.component.spec.ts都会在控制台提示错误: Failed: Unexpected directive 'HomeModuleComponent' imported by the module 'DynamicTestModule' Error: Unexpected directive 'HomeModuleComponent' impor

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

 Failed: Unexpected directive 'HomeModuleComponent' imported by the module 'DynamicTestModule'
 Error: Unexpected directive 'HomeModuleComponent' imported by the module 'DynamicTestModule'
我在测试床中导入了HomeModuleComponent

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

有人能帮我解决这个问题吗?

HomeModuleComponent
组件
而不是
模块
,因此它必须在声明中:

TestBed.configureTestingModule({
  declarations: [AppComponent, HomeModuleComponent],
  imports : [ ]
});
然后您可以创建要测试的组件

TestBed.createComponent(AppComponent);

在我的测试规范中,我错误地导入了
服务
,而没有提供它。我也犯了同样的错误


服务
添加回
提供者
数组解决了我的错误。

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