我有以下测试用例,
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)
我尝试将name属性添加到address字段,但是不走运
答案 0 :(得分:0)
在测试类中导入MatFormFieldModule
即可。