angular-cli 测试在创建共享模块后失败

angular-cli tests fail after creating shared module

我只是添加了一个带有 SharedComponent 的共享模块,并在主应用程序组件中使用了该组件:

<acs-shared></acs-shared>

该组件所做的只是在组件上使用 属性 显示 'Hello, world!':

<h1>Hello, {{name}}!</h1>

当 运行 与 npm start 的项目时,这一切工作正常,但现在 运行 npm test 失败,$(npm bin)/karma start ./karma.conf.js 也是如此。第一个失败是它无法创建组件,因为它无法识别 'acs-shared' 元素。是否需要做一些特别的事情来测试使用其他组件或模块的组件?

Chrome 54.0.2840 (Windows 10 0.0.0) App: AngularCliStarter should create the app FAILED
    'acs-shared' is not a known element:

该项目在 github 可用:https://github.com/JasonGoemaat/angular-cli-starter

您需要将 SharedModule 导入到 TestBed 配置中。您使用 TestBed 所做的就像为测试环境

从头开始​​ 配置 @NgModule
TestBed.configureTestingModule({
  imports: [ SharedModule ],
  declarations: [
    AppComponent
  ],
  providers: []
});