angular - 角度形式的业力单元测试给出错误“”
问题描述
我有以下测试用例,
fit('When Address is not having any entry, the address field should be marked as Invalid', () => {
component.basicBookFormGroup.patchValue({
bookName: 'My Site Name',
bookOrg: 'Organization',
bookAddress: '',
});
fixture.detectChanges();
const addressField: DebugElement = fixture.debugElement.query(By.css('#bookAddressTxtArea'));
expect(addressField.nativeElement.Invalid).toBeTruthy();
});
在 HTML 地址字段中标记为必填项,因此我期望 Invalid 属性对于上述内容应该为 true。
<mat-form-field appearance="outline" class="full-width-input">
<mat-label>{{translate.address}}</mat-label>
<textarea id="bookAddressTxtArea" autocomplete="off" matInput placeholder="{{translate.address}}" formControlName="bookAddress"
required></textarea>
</mat-form-field>
但是我收到以下错误:
Error: No value accessor for form control with unspecified name attribute
at _throwError (node_modules/@angular/forms/fesm5/forms.js:2094:1)
at setUpControl (node_modules/@angular/forms/fesm5/forms.js:2004:1)
at FormControlDirective.push../node_modules/@angular/forms/fesm5/forms.js.FormControlDirective.ngOnChanges (node_modules/@angular/forms/fesm5/forms.js:4951:1)
at checkAndUpdateDirectiveInline (node_modules/@angular/core/fesm5/core.js:20661:1)
at checkAndUpdateNodeInline (node_modules/@angular/core/fesm5/core.js:21929:1)
at checkAndUpdateNode (node_modules/@angular/core/fesm5/core.js:21891:1)
at debugCheckAndUpdateNode (node_modules/@angular/core/fesm5/core.js:22525:1)
at debugCheckDirectivesFn (node_modules/@angular/core/fesm5/core.js:22485:1)
我尝试将名称属性添加到地址字段,没有运气
解决方案
在测试类中导入MatFormFieldModule
将起作用。
推荐阅读
- c - elf文件格式,顺序是什么?
- python - 无法使用 python 请求库下载压缩的 csv 文件
- blazor - 如何在带有 blazor 的 Razor 类库中使用新的 NET 6 静态资产
- angular - 路由未重定向到预期页面
- r - 如何将 colMeans 结果添加到 R 中保留 data.frame 的最后一行?
- go - 如何在 Go 中编写双向映射?
- docker - 重命名devilbox所在的文件夹后,数据库消失了。我该如何移动它们?
- android - Flutter 使用 huawei_map 报错“尝试创建未注册类型的平台视图:com.huawei.hms.flutter.map/map”
- postgresql - POSTGRES:预期 DEFAULT 表达式的类型为 varchar,但 '0' 的类型为 int
- java - Android 处理程序 java 到 kotlin