如何在Angular 4规格文件中模拟nativeElement.focus() [英] How to mock a nativeElement.focus() in Angular 4 spec file
本文介绍了如何在Angular 4规格文件中模拟nativeElement.focus()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一种使用下面定义的 ElementRef
的方法.
I have a method which uses an ElementRef
which is defined below.
@ViewChild('idNaicsRef') idNaicsRef: ElementRef;
然后
ElementRef
使用 .nativeElement.focus()
设置焦点.
该方法在运行规范时失败,提示未定义是对象"
The method fails while running the spec, saying 'undefined is an object'
推荐答案
这应该有效.这只会创建一个间谍对象,然后您可以使用所需的任何对象填充它,因此甚至可以检查它是否在单元测试中被调用.
this should work. this just creates a spy object and then you can populate it with whatever you want, so you could even check if it was called in your unit test.
import createSpyObj = jasmine.createSpyObj;
comp.idNaicsRef = createSpyObj('idNaicsRef', ['nativeElement']);
comp.idNaicsRef.nativeElement = { focus: () => { }};
comp
是对要测试的组件的引用.
comp
is the reference to the component you are testing.
createSpyObj
来自茉莉花导入
这篇关于如何在Angular 4规格文件中模拟nativeElement.focus()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文