为什么mouseover或mouseout事件不会从测试中触发?

时间:2012-10-24 11:29:33

标签: jquery coffeescript qunit

我有以下QUnit测试:

test 'can view saved status and undo if needed', ->
    save_link = $('.save-link')
    my_profile_div = $('.searchbar .span5')
    undo_save = $('#undo-save')

    my_profile_div.mouseover()
    equal(undo_save.css('visibility'), 'hidden')
    my_profile_div.mouseout()

    save_link.click()

    my_profile_div.mouseover()
    equal(undo_save.css('visibility'), 'visible')   
    my_profile_div.mouseout()
    equal(undo_save.css('visibility'), 'hidden')

单击链接,然后期望元素my_profile_div存在一些悬停行为。不幸的是,mouseover和mouseout事件不会从测试中触发。使用此实现时,我没有收到任何警报:

$('.searchbar .span5').mouseover -> 
    alert('mouseover')

$('.searchbar .span5').mouseout -> 
    alert('mouseout')

在浏览器中,我确实收到了警报,但是当我运行上面的测试时却没有,这让我觉得我没有正确地触发事件。我也试过trigger('mouseover')无济于事。

感谢您花时间阅读本文,伙计们。我非常感谢你的帮助。

注意:测试是针对所需的行为。实现只是为了确保测试正在按照预期进行。

1 个答案:

答案 0 :(得分:-1)

我宁愿使用触发器来发起事件。

$('targetElement').trigger('eventName');