[英]Angular test - how to mock document.getElementbyId in angular test?
I have below two lines in one of the functions in angular component.我在 angular 组件中的一个功能中有以下两行。 My angular tests are failing throwing the error TypeError: Cannot set properties of null (setting 'value').
我的 angular 测试失败,抛出错误 TypeError: Cannot set properties of null (setting 'value')。
test.ts
function reset(){
(<HTMLInputElement>document.getElementById('test1')).value = '';
this.nameCtrl.setValue(null);
}
test.spec
fixture.debugElement.componentInstance.test1.value='';
fixture.debugElement.componentInstance.nameCtrl.setValue(null);
I tried to mock the lines in rest using the above by it still fails.我尝试使用上述方法模拟 rest 中的行,但它仍然失败。 Please help
请帮忙
Consider using the Angular ViewChild
decorator instead of document.getElementById
for accessing the test1 DOM element.考虑使用 Angular
ViewChild
装饰器而不是document.getElementById
来访问 test1 DOM 元素。
See an example here: https://www.digitalocean.com/community/tutorials/angular-viewchild-access-component在此处查看示例: https://www.digitalocean.com/community/tutorials/angular-viewchild-access-component
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.