我正在为一个 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)