[英]How best to unit-test an events abstraction?
I have two questions: 我有两个问题:
1.) How best to unit-test an event listener? 1.)如何最好地对事件侦听器进行单元测试? 2.) Have I properly understood isHostMethod?
2.)我是否正确理解isHostMethod?
I'm trying to unit-test an event listener abstraction but the only way I can think to test it is against the window 'load' event. 我正在尝试对事件侦听器抽象进行单元测试,但是我认为可以测试它的唯一方法是针对窗口“加载”事件。
I've a code example here: https://gist.github.com/1502326 我在这里有一个代码示例: https : //gist.github.com/1502326
Also, I'm using David Mark's (@cinsoft) isHostMethod to detect the relevant host object support but was wondering if I had properly understood the concepts or not? 另外,我正在使用David Mark(@cinsoft)的isHostMethod来检测相关的宿主对象支持,但想知道我是否正确理解了这些概念? So for example, am I right in thinking that a host object/method should be available under the following conditions (although I understand that these 'conditions' are unreliable):
因此,举例来说,我是否认为主机对象/方法应该在以下条件下可用(尽管我知道这些“条件”是不可靠的)是正确的:
If any of these conditions are true then that (again, unreliably) should mean that the specified host method is available to use. 如果这些条件中的任何一个成立,则(再次,不可靠地) 应意味着可以使用指定的宿主方法。
But even still the host object/method might be implemented differently to how the specification dictates it should be and so it would be more accurate to do full 'feature detection' where I create the object and see if an event is triggered (or some similar test)? 但是,即使主机对象/方法的实现方式仍可能与规范指示的方式不同,因此在我创建对象并查看事件是否被触发(或类似事件)的情况下执行完整的“功能检测”会更加准确。测试)?
Thanks for any advice regarding both these enquiries. 感谢您对这两个查询的任何建议。
The simplest way is to use a synthetic click event. 最简单的方法是使用综合点击事件。 Given the code in your gist, you could test with:
给定要点中的代码,您可以使用以下命令进行测试:
var evt;
events.add( document.body, "click", function() {
// swap this for an assert(true, "message");
console.log( true, "supported" );
});
// IE nodes support a click() method
if ( document.body.click ) {
document.body.click();
} else {
// Standards based events have to be created, initialized and dispatched.
evt = document.createEvent("MouseEvents");
evt.initMouseEvent(
"click", true, true, window, 0, 0, 0, 0, 0, false, false, false, false, 0, null
);
document.body.dispatchEvent( evt );
}
See: http://jsfiddle.net/rwaldron/CR4FC/ 请参阅: http : //jsfiddle.net/rwaldron/CR4FC/
Tested in: IE6,7,8; 测试于:IE6,7,8; Chrome 17;
铬17; Firefox 10;
Firefox 10; Safari 5.1.2;
Safari 5.1.2; Opera 11
歌剧11
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.