Angular FormControl中的假阴性测试值
我有一个Angular FormControl中的假阴性测试值,angular,jasmine,karma-jasmine,Angular,Jasmine,Karma Jasmine,我有一个Angular组件,它编辑通过@Input()加载的对象中的数据: 组件的模板为: <div class="container-fluid"> <div class="row"> <form class="form-inline" [formGroup]="editForm"> <div class="col-xl-3"> <label for="editFieldInput
Angular
组件,它编辑通过@Input()
加载的对象中的数据:
组件的模板为:
<div class="container-fluid">
<div class="row">
<form class="form-inline"
[formGroup]="editForm">
<div class="col-xl-3">
<label for="editFieldInput"
class="sr-only">Field Value</label>
<input id="editFieldInput"
type="text"
class="form-control-sm"
placeholder="Field"
formControlName="editFieldControl">
</div>
</form>
</div>
</div>
我可以成功地测试(并失败)onInit()
逻辑,因此表单控件的值被设置为obj.field
的值。然后,我尝试生成onClear()
方法的失败测试:
it('should reset FormControls to their original value', () => {
fixture.whenStable().then(() => {
component.editForm.controls.editFieldControl.setValue('test');
component.onClear();
fixture.detectChanges();
fixture.whenStable().then(() => {
expect(component.editForm.controls.editFieldControl.value).toBe(component.obj.field);
})
});
});
由于onClear()
是一个空方法,我希望这个测试失败,但它通过了。我玩了一会儿,试图产生一个失败,但没有成功,最终尝试用以下内容取代最初的预期:
expect(component.editForm.controls.editPackControl.value).toBe('111');
expect(component.editForm.controls.editPackControl.value).toBe('000');
奇怪的是,这个不可能的期望也过去了。我尝试重新启动
karma
服务器,然后重新启动节点
本身,但是错误的否定仍然存在。您是否尝试过使用类似于it('should reset FormControls to the private value',async(()=>{
?我想知道在下面的代码中,这个指的是什么。toBe(this.obj.field)
@yurzui“this”输入错误,不是在实际代码中。可能是在我将实际组件代码简化为可读性更强的代码时发生的。我以前尝试过注入async
函数,但没有成功。让我们尝试重新生成它。请看一分钟,下面是失败的测试
it('should reset FormControls to their original value', () => {
fixture.whenStable().then(() => {
component.editForm.controls.editFieldControl.setValue('test');
component.onClear();
fixture.detectChanges();
fixture.whenStable().then(() => {
expect(component.editForm.controls.editFieldControl.value).toBe(component.obj.field);
})
});
});
expect(component.editForm.controls.editPackControl.value).toBe('111');
expect(component.editForm.controls.editPackControl.value).toBe('000');