小编dev*_*357的帖子

即使在声明组件存根之后,Angular 单元测试也会显示 NG0304“组件不是已知元素”类型的错误警告

我正在为一个 Angular 组件编写单元测试,该组件使用多个子组件,这些子组件通过以下方式与 @ViewChild({}) 一起使用:

模板:

...
<app-app-form></app-app-form>
<app-terms-form></app-terms-form>
...
Run Code Online (Sandbox Code Playgroud)

模型:

...
@ViewChild(AppFormComponent, {static: true}) protected appForm: AppFormComponent;
@ViewChild(TermsFormComponent, {static: true}) protected termsForm: TermsFormComponent;
...
Run Code Online (Sandbox Code Playgroud)

我总共使用了六个这样的组件。出于测试目的,我以这种方式声明组件存根:

@Component({
  selector: 'app-terms-form',
  template: '',
  providers: [
    { provide: TermsFormComponent, useClass: TermsFormStubComponent }
  ]
})
class TermsFormStubComponent implements Partial<IFormComp<any>> {
...
}
Run Code Online (Sandbox Code Playgroud)

当我运行测试时,我收到所有使用的子组件的错​​误警告消息,尽管我在 TestBed 中声明了它们:

ERROR: 'NG0304: 'app-terms-form' is not a known element (used in the 'CreateLicenseComponent' component template):
1. If 'app-terms-form' is an Angular component, then verify that it is a part of …
Run Code Online (Sandbox Code Playgroud)

testing unit-testing angular

6
推荐指数
0
解决办法
726
查看次数

标签 统计

angular ×1

testing ×1

unit-testing ×1