开玩笑单元测试"Cannot find control with unspecified name attribute"



我正在为我的"动态ish";自动表单创建者和我刚刚为下拉列表制作了一个组件:

<div>
<select [name]="propertyName" [formControl]="customControl" [id]="propertyName">
<option *ngFor="let option of options" [value]="option.id">
{{ option.label }}
</option>
</select>
</div>

代码也非常简单:

export class DropdownComponent {
@Input() public customControl: AbstractControl;
@Input() public propertyName: string;
@Input() public options: any[];
}

现在,当我运行jest来测试这个组件时(这个测试是由ng-cli创建的,所以默认情况下,我只添加了ReactiveFormsModule作为导入(,它无法创建组件。

× should create (363 ms)
● DropdownComponent › should create
Cannot find control with unspecified name attribute
at _throwError (../packages/forms/src/directives/shared.ts:150:9)
at setUpControl (../packages/forms/src/directives/shared.ts:37:19)
at ...

版本:
Angular:10.1.6
Jest:26.6.3
Angular CLI:10.1.7
节点:14.4.0
操作系统:win32 x64

由于谷歌给了我10个结果(都是误导性的或不可能的(,我尝试添加/删除部分代码,结果发现customControl需要用一个值初始化,尽管它将从外部接收一个表单控件(如@Input(。这不是<input>标签的问题,而是<select>标签的问题!

@Input() public customControl: AbstractControl = new FormControl('');

相关内容

最新更新