没有带名称的表单控件的值访问器......-用于mat-select控件 [英] No value accessor for form control with name... for mat-select controls
问题描述
我正在使用茉莉花和业力对角度6 应用进行一些单元测试,以验证 formGroup >字段有效.我在使用垫选择控件时遇到问题.当我运行测试用例时,因果报应给我一个错误,提示Error: No value accessor for form control with name: 'importId'
.顺便说一句,该组件可以正常工作.
I am making some unit test with jasmine and karma for an angular 6 app that validate if a formGroup field is valid. I am experiencing problems with mat-select control. when I run the test case, Karma fires me an error saying Error: No value accessor for form control with name: 'importId'
. By the way, the component works fine as I expected.
这是我的组件:
import {Component, Inject, OnInit} from '@angular/core';
import {MAT_DIALOG_DATA, MatDialogRef} from "@angular/material";
import {FormBuilder, FormControl, FormGroup, Validators} from "@angular/forms";
@Component({
selector: 'app-my-component',
templateUrl: './my.component.html',
styleUrls: ['./my.component.css']
})
export class MyComponent implements OnInit {
modelForm: FormGroup;
imps;
constructor(
public dialogRef: MatDialogRef<MyComponent>,
@Inject(MAT_DIALOG_DATA) public data: any) {
this.imps = data['imp'];
}
ngOnInit() {
this.modelForm = new FormGroup({
name: new FormControl(null, Validators.required),
importId: new FormControl(null, Validators.required),
});
}
}
我的HTML模板如下:
My HTML template looks like this:
<mat-dialog-content>
<form [formGroup]="modelForm">
<mat-form-field>
<input matInput
placeholder="Name"
formControlName="name">
</mat-form-field>
<mat-form-field>
<mat-select placeholder="Source import"
formControlName="importId">
<mat-option *ngFor="let imp of imps" [value]="imp.uuid">
{{imp.label}}
</mat-option>
</mat-select>
</mat-form-field>
</form>
</mat-dialog-content>
<mat-dialog-actions>
<button mat-raised-button color="primary" [disabled]="!modelForm.valid" (click)="someFakeFunction()">Create</button>
<button mat-raised-button (click)="dialogRef.close()">Cancel</button>
</mat-dialog-actions>
最后,这是我的单元测试:
Finally, this is my unit test:
import {async, ComponentFixture, TestBed} from '@angular/core/testing';
import {MockMatDialogData, MockMatDialogRef} from '@testing/mock/material';
import {MyComponent} from './evaluation-wizard.component';
import {MAT_DIALOG_DATA, MatDialogRef} from "@angular/material";
import {FormsModule, ReactiveFormsModule} from "@angular/forms";
import {NO_ERRORS_SCHEMA} from "@angular/core";
describe('MyComponent', () => {
let component: MyComponent;
let fixture: ComponentFixture<MyComponent>;
beforeEach(async(() => {
TestBed.configureTestingModule({
declarations: [MyComponent],
imports: [ReactiveFormsModule, FormsModule],
providers: [
{provide: MatDialogRef, useValue: MockMatDialogRef},
{provide: MAT_DIALOG_DATA, useClass: MockMatDialogData}
],
schemas: [NO_ERRORS_SCHEMA],
})
.compileComponents();
}));
beforeEach(() => {
fixture = TestBed.createComponent(MyComponent);
component = fixture.componentInstance;
component.ngOnInit();
fixture.detectChanges();
});
it('should create', () => {
expect(component).toBeTruthy();
});
describe('Form validation', () => {
it('form invalid when empty ', function () {
expect(component.modelForm.valid).toBeFalsy();
});
it('name field validity ', () => {
let name = component.modelForm.controls['name'];
expect(name.valid).toBeFalsy();
let errors = {};
errors = name.errors || {};
expect(errors['required']).toBeTruthy();
name.setValue("test");
errors = name.errors || {};
expect(errors['required']).toBeTruthy();
});
});
});
我无法使它起作用,有什么建议是我所缺少的吗?
I can't make that works, any suggestions what I am missing?
推荐答案
您没有在测试模块中导入材料模块.
You're not importing the material modules in your testing module.
因此,mat-form-field
,mat-select
等仅被Angular视为未知元素(因为您通过使用NO_ERRORS_SCHEMA
告诉它这样做).
So mat-form-field
, mat-select
, etc. are just treated as unknown elements by Angular (since you told it to do so by using NO_ERRORS_SCHEMA
).
这篇关于没有带名称的表单控件的值访问器......-用于mat-select控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!