[英]TriggerEventHandler in unit testing angular 4
I had an element 我有一个元素
<button class="next-btn" (mouseup)="someMethod()">Next</button>
I want to simulate mouseup event in testing and i do this 我想在测试中模拟mouseup事件,我这样做
const nextBtnElem = fixture.debugElement.nativeElement;
const elem = nextBtnElem.getElementsByTagName('button')[1]
elem.triggerEventHandler('mouseup', null);
it doesn't work, but if i change it like this 它不起作用,但是如果我这样更改它
const nextBtnElem = fixture.debugElement.query(By.css('.next-btn');
nextBtnElem.triggerEventHandler('mouseup', null);
Now its working. 现在可以正常工作了。 I can't understand why first optional doesn't work? 我不明白为什么第一个可选选项不起作用?
triggerEventHandler
is a function that exists on Angular's DebugElement
. triggerEventHandler
是Angular的DebugElement
上存在的DebugElement
。 In your first code snippet you are calling triggerEventHandler
on a DOM object, that does not provide this functionality. 在您的第一个代码段中,您正在不提供此功能的DOM对象上调用triggerEventHandler
。 On DOM objects you could use dispatchEvent but you must call fixture.detectChanges();
在DOM对象上,您可以使用dispatchEvent,但是必须调用fixture.detectChanges();
between triggering your event and your assertions then. 然后触发事件和声明。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.