dev*_*357 6 testing unit-testing angular
我正在为一个 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 an @NgModule where this component is declared.
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 an @NgModule where this component is declared.
Run Code Online (Sandbox Code Playgroud)
如您所见,我已经添加了 CUSTOM_ELEMENTS_SCHEMA 和 NO_ERRORS_SCHEMA,还分别尝试了每个。但这不起作用。我不应该首先添加它们,因为所有需要的元素都在“声明”数组中。我仍然收到所有六个子组件的错误警告
我的测试全部成功运行,并且通过调试我已经确认组件存根可用。我可以忽略这些错误警告,但它们仍然很烦人,因为我将添加更多组件,例如所展示的组件。有谁知道如何干净地解决这个问题?