如何在单元测试中设置html输入元素的文本内容
我正在运行angular应用程序的单元测试,我想通过单元测试设置此输入元素的文本内容。我正在使用jasmine如何在单元测试中设置html输入元素的文本内容,html,angular,typescript,unit-testing,Html,Angular,Typescript,Unit Testing,我正在运行angular应用程序的单元测试,我想通过单元测试设置此输入元素的文本内容。我正在使用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
<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;
formData.nativeElement.dispatchEvent(new Event('input'));
expect(formData.nativeElement.value).toBe(22);
谢谢Chrillewoodz,但它没有设置输入元素的文本内容,在每个语句后都尝试fixture.detectchanges,不是吗work@karansys我不认为你应该测试textContent
,而是测试值。谢谢:)这样行吗,你能编辑你的答案吗,所以我会把它标记为正确的
let formData = fixture.debugElement.query(By.css('#accountid'));
formData.nativeElement.value = 22;
formData.nativeElement.dispatchEvent(new Event('input'));
expect(formData.nativeElement.value).toBe(22);