Javascript 如何使用桌面浏览器测试触摸事件?
我想为触摸事件编写一些测试用例。在我的代码中,我有以下逻辑来决定它是否在触摸设备上Javascript 如何使用桌面浏览器测试触摸事件?,javascript,unit-testing,sinon,touch-event,Javascript,Unit Testing,Sinon,Touch Event,我想为触摸事件编写一些测试用例。在我的代码中,我有以下逻辑来决定它是否在触摸设备上 if(document.documentElement.ontouchstart !== undefined) { //content that I want to write test cases about } 现在我想假装document.documentElement.ontouchstart不是未定义的,这样内部逻辑将被执行 我尝试使用Sinon.js的stub为document.documentE
if(document.documentElement.ontouchstart !== undefined)
{
//content that I want to write test cases about
}
现在我想假装document.documentElement.ontouchstart
不是未定义的
,这样内部逻辑将被执行
我尝试使用Sinon.js
的stub
为document.documentElement.ontouchstart
分配一个空函数。但它会抛出错误指示“TypeError:试图将未定义的属性包装到TouchStart
作为函数”,因为文档。documentElement
在浏览器中没有这样的属性
我真的不想在测试中编写类似于:document.documentElement.ontouchstart=function(){..}
的东西,因为它会影响其他测试用例。当然,我可以存储它的原始值,并在测试用例结束时恢复该值。但我想问,有没有更优雅的方式来实现这一点
如何进入
if
语句?使用Chrome的设备工具,通过激活开发者工具中的此按钮进行切换
作为图像
这允许您像在移动环境中一样进行调试。您可以调用/触发onmousedown事件ontouchstart@ABUdhay但是我想检查
if
中的逻辑。如果语句变为true,则触发onmousedown
不会让语句变为true。这就是Dom中基本上不存在的内容。。您已经向DomeElement对象添加了一个方法,比如document.documentElement.ontouchstart=function(){//这里您必须调用onmousedown或trigger mousedown事件处理程序method}@ABUdhay是的,这就是我现在写的。希望还有其他选项。这样,document.documentElement.ontouchstart
仍然是未定义的
。即使它是以这种方式定义的,我也不能告诉qunit
以这种方式运行测试用例。