Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/27.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Angular 角度反应形式检查测试中是否存在FormControl_Angular_Jasmine_Angular Reactive Forms - Fatal编程技术网

Angular 角度反应形式检查测试中是否存在FormControl

Angular 角度反应形式检查测试中是否存在FormControl,angular,jasmine,angular-reactive-forms,Angular,Jasmine,Angular Reactive Forms,我在Angular 5中有一个反应形式,它在工作。此表单有一个e.q.输入字段和一个复选框 <div class="form-group" *ngIf="!myForm.controls.myCheckbox.value"> <input class="form-control" formControlName="myField" </div> <div class="form-group"> <input type="check

我在Angular 5中有一个反应形式,它在工作。此表单有一个e.q.输入字段和一个复选框

<div class="form-group" *ngIf="!myForm.controls.myCheckbox.value">
    <input class="form-control" formControlName="myField"
</div>

<div class="form-group">
    <input type="checkbox" formControlName="myCheckbox">
</div>
预期FormControl(…)为falsy“myField not existing”


我现在的问题是,在jasmine测试中单击复选框时,如何测试表单控件“myField”是否可见?

好的,我找到了一个解决方案。首先,我需要通过以下方式获取元素:

const el = fixture.debugElement.nativeElement;
let myField= el.querySelector('input[formControlName=myField]');
expect(myField).toBeTruthy();

let myCheckboxControl= component.driverForm.controls['myCheckbox'];
expect(myCheckboxControl.value).toEqual(false);
//set checkbox state to true
myCheckboxControl.setValue(true);

fixture.detectChanges();
更新复选框后,我需要再次获取输入字段:

myField = el.querySelector('input[formControlName=myField]');
expect(myField).toBeNull("myField not existing");
myField = el.querySelector('input[formControlName=myField]');
expect(myField).toBeNull("myField not existing");