我正在尝试测试通过键盘输入值的事件。我的问题是在为输入字段设置值之后,当我打印它时打印它,但是当我在 whenStable() 中打印它时它打印为空。我想知道为什么这个值在 Whitstable() 函数中被重置。我该如何解决?
我提到了:Updating input html field from within an Angular 2 test编写这个测试用例。
it('Test input field value. ', async () => {
const divDescription = fixture.debugElement.query(By.css('#costDescription'));
divDescription.nativeElement.value = 'text';
divDescription.nativeElement.dispatchEvent(new Event('input'));
fixture.detectChanges();
console.log('sendInput : ', divDescription.nativeElement.value); // prints 'text'
fixture.whenStable().then(() => {
console.log('sendInput : ', divDescription.nativeElement.value); // prints ''
expect(divDescription.nativeElement.value).toContain('text');
});
});
最佳答案
删除 WhenStable() 会发生这种情况。
it('Test input field value. ', async () => {
const divDescription = fixture.debugElement.query(By.css('#costDescription'));
divDescription.nativeElement.value = 'text';
divDescription.nativeElement.dispatchEvent(new Event('input'));
fixture.detectChanges();
expect(divDescription.nativeElement.value).toContain('text');
});
关于javascript - Angular 4( karma )测试输入字段的输入值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52325439/