问题描述
我刚刚进入QUnit。我使用jQuery(用1.4.2和1.5.1测试)和最新的QUnit。我可以在单次测试中触发事件,但是之后的任何测试都会失败。这是一个简化的复制: //测试中的代码 - #Test1只是一个空的div
$(文件).ready(function(){
$('#Test1')。mouseenter(function(e){console.log('ENTER');});
$('#Test1' ).mouseleave(function(e){console.log('LEAVE');});
});
// test
test('enter',function(){
$('#Test1')。mouseenter();
});
test('leave',function(){
$('#Test1')。mouseleave();
});
当我运行测试时,控制台只输入ENTER。但是,如果我使用单个测试...
test('enterleave',function(){
$ ('#Test1')。mouseenter();
$('#Test1')。mouseleave();
});
...控制台输出ENTER和LEAVE。我试过使用QUnit的triggerEvent,jQuery.trigger等没有用。这个问题在多个浏览器上重现。我正在正确地测试事件吗?
完全重申:。
QUnit似乎清除元素的事件绑定一旦发生触发。我通过将我的init代码移动到QUnit设置中来解决了这个问题。我不知道这是否是一个好的做法,但似乎已经解决了这个问题。我也修复了以上样本:。
I'm new to QUnit. I'm using jQuery (tested with 1.4.2 and 1.5.1) and the latest QUnit. I can trigger events just fine in a single test, but any test afterwards fails. Here's a simplified repro:
// code under test - "#Test1" is just an empty div
$(document).ready(function() {
$('#Test1').mouseenter(function(e) { console.log('ENTER');});
$('#Test1').mouseleave(function(e) { console.log('LEAVE');});
});
// tests
test('enter', function() {
$('#Test1').mouseenter();
});
test('leave', function() {
$('#Test1').mouseleave();
});
When I run the tests, the console outputs only ENTER. However, if I use a single test...
test('enterleave', function() {
$('#Test1').mouseenter();
$('#Test1').mouseleave();
});
...the console outputs ENTER and LEAVE. I've tried using QUnit's triggerEvent, jQuery.trigger, etc. to no avail. This issue repros on multiple browsers. Am I testing events correctly?
Full repro here: http://jsbin.com/obehu5/edit.
QUnit appears to be clearing the event binding for the element once a trigger occurs. I fixed this issue by moving my "init" code into the QUnit setup. I'm not sure if this is a good practice or not but it seems to have resolved the issue. I also fixed the sample from above: http://jsbin.com/obehu5/5/.
这篇关于事件在QUnit测试中未触发的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!