如何在单元测试中设置html input元素的文本内容 [英] How to set text content of html input element in unit test
本文介绍了如何在单元测试中设置html input元素的文本内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在为 angular 应用程序运行单元测试,我想通过单元测试设置此输入元素的文本内容.我正在使用 jasmine
I am running unit test for angular app, I want to set the text content of this input element via unit test.I am using jasmine
<input type="text" id="accountid" class="form-control col-sm-3" [(ngModel)]="record.accountid" name="accountid" required>
我试过了,但没有用
let formData = fixture.debugElement.query(By.css('#accountid'));
formData.nativeElement.value = 22;
fixture.detectChanges();
console.log(formData.nativeElement.textContent);//expected to print 22
expect(formData.nativeElement.textContent).toBe(22);//fails
推荐答案
你必须在设置它的值后调度一个 input
事件:
You must dispatch an input
event after setting the value of it:
let formData = fixture.debugElement.query(By.css('#accountid'));
formData.nativeElement.value = 22;
formData.nativeElement.dispatchEvent(new Event('input'));
expect(formData.nativeElement.value).toBe(22);
这篇关于如何在单元测试中设置html input元素的文本内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文