在父组件中使用子组件时出错 - Angular 2单元测试(KarmaJasmine)

时间:2017-06-01 10:01:16

标签: angular unit-testing jasmine karma-jasmine

在父级中使用子组件时

template parse error

我有两个组成部分。

  1. CardComponent
  2. InfoComponent

我的CardComponent的HTML

     <div class="footer">
       <full-info></full-info>
     </div>

CardComponent的规范文件:

 beforeEach(async(() => {
TestBed.configureTestingModule({
  declarations: [InfoComponent, CardComponent ],
  imports: [
      FormsModule,
      MaterialModule.forRoot()
  ],
  schemas: [ NO_ERRORS_SCHEMA ], //Used CUSTOM_ERRORS_SCHEMA also
})
.compileComponents();
  }));

我的InfoComponent ts代码:

   import { Component, Input } from '@angular/core';


   @Component({
            selector: 'full-info',
            templateUrl: './full-info.component.html',
            styleUrls: ['./full-info.component.css']
            })

   export class InfoComponent {
     public fullInfo;
    }

InfoComponent规范声明:

     beforeEach(async(() => {
TestBed.configureTestingModule({
  declarations: [ InfoComponent ],
  imports: [
      FormsModule,
      MaterialModule.forRoot()
  ],
  schemas: [ NO_ERRORS_SCHEMA ]
})
.compileComponents();
   }));

当我为这两个组件运行测试用例单独时,它运行正常。

当我一次运行这些测试用例时,我收到错误:

    'full-info' is not a known element:
1. If 'full-info' is an Angular component, then verify that it is part of this module.
2. If 'full-info' is a Web Component then add "CUSTOM_ELEMENTS_SCHEMA" to the '@NgModule.schemas' of this component to suppress this message. ("
  </div>
  <div class="footer">
    [ERROR ->]<full-info></full-info>
  </div>

我已在InfoComponent中导入CardComponent,即使这样我也收到此错误。我无法找到问题。我知道那对我来说会很棒。

1 个答案:

答案 0 :(得分:0)

两个组件是否共享完全相同的模块? 模块导入好吗? 是否forRoot()非常必要?

来自documentation

  

仅在根应用程序模块AppModule中调用。调用   它在任何其他模块中,特别是在延迟加载的模块中,是   与意图相反并且可能产生运行时错误。

     

记得导入结果;不要将它添加到任何其他@NgModule   列表。

尝试删除forRoot()并查看错误是否仍然存在。

希望我有所帮助,让我知道它是怎么回事!