[英]Karma Unit Testing for angular Forms gives the error “”
我有以下測試用例,
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字段,但是不走運
在測試類中導入MatFormFieldModule
將起作用。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.