我正在测试这个HTML表单:
<form name="editForm" role="form" novalidate (ngSubmit)="save()" #editForm="ngForm">
<input #inputValid type="text" class="form-control" name="nombre" id="field_nombre"
[(ngModel)]="paciente.nombre" required/>
我测试输入的有效性,这意味着如果它是空的,它应该失败。
我在我的组件中使用ViewChild引用了input
元素,如下所示:
&GT;
export class PacienteDialogComponent implements OnInit {
@ViewChild('inputValid') inputValid: ElementRef;
......
我测试它是这样的:
fit ('If blank it should be invalid', async(() => {
comp.inputValid.setValue('');
//comp.inputValid.controls['paciente.nombre'].setValue('');
//OUTPUTS CANNOT READ paciente.nombre of undefined
expect(inputElement.valid).toBeFalsy();
}));
我得到的错误是:
SetValue不是函数。
如果我使用控件,则错误就像在行的注释中一样:
无法读取未定义的属性。
如何测试此输入的有效性?该形状是由角度模板驱动的。
答案 0 :(得分:1)
首先,我不明白为什么你需要手动检查这个,如果你有一个&#34;要求&#34;在输入控件中。
无论如何,你不应该通过ViewChild组件检查,你可以直接检查绑定到表单(aka paciente.nombre)的类的字段。
但是,如果您有理由访问html元素,则必须使用
comp.inputValid.nativeElement.value = .....
希望它有所帮助!