【发布时间】:2021-09-25 08:04:10
【问题描述】:
我正在尝试为复选框更改编写简单的测试函数,但我无法运行它。 错误是 Spec 没有期望。并且处理函数不在代码覆盖范围内。
模板:
<input type="checkbox" (change)="handleChange($event)">
处理函数:
handleChange(event:any){
if(event.target.checked){
this.myVariable= true;
}else{
this.myVariable = false;
}
}
测试用例:
it('checkbox change should set the myVariable value',fakeAsync(()=>{
myCheckBox = fixture.debugElement.query(By.css('#myCheckBox'));
spyOn(component, 'handleChange');
myCheckBox.triggerEventHandler('change',{target: myCheckBox.nativeElement});
tick();
fixture.detectChanges();
expect(component.myVariable).toBetrue;
}));
我应该如何编写 Jasmine 测试用例来检查这个功能并覆盖 if 语句。
【问题讨论】:
-
您需要 1) 在测试中找到输入,2-a) 传递一个包含 target.checked 的模拟事件来满足第一个条件,2-b) 传递另一个包含 event.target 的模拟事件.检查为假
标签: angular typescript jasmine karma-jasmine angular-test