【发布时间】:2018-05-17 10:19:23
【问题描述】:
我正在测试这个 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 元素,如下所示:
>
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 不是函数。
如果我使用控件,错误就像在该行的注释中一样:
无法读取未定义的属性。
如何测试此输入的有效性?表单是模板驱动的角度。
【问题讨论】:
-
你为什么不检查
paciente.nombre?不需要 viewChild -
怎么样?获取 HTMLElement 引用的唯一方法是使用 ViewChld
标签: javascript html angular setvalue