等待QUnit测试

Len*_*bro 15 javascript jquery qunit

我有jQuery代码,当我点击它首先隐藏的链接然后删除一些HTML,如下所示:

$(this).parent().parent().hide('slow', function () {
    $(this).remove();
});
Run Code Online (Sandbox Code Playgroud)

我想进行一个QUnit测试,确保删除有问题的HTML:

$(thelink).click();

// Check that it is gone, by finding the first item in the list
entity = input.form.find('.recurrenceinput_occurrences .occurrence span.action a')[0];
// And make sure it's NOT the deleted one:
ok(entity.attributes.date.value !== "20110413T000000");
Run Code Online (Sandbox Code Playgroud)

问题当然是在隐藏动画运行结束之前运行ok()测试,因此尚未删除有问题的HTML,并且测试失败.

我已经尝试了各种方法来延迟/停止测试一秒左右,但似乎没有任何工作.最明显的一个是使用asynTest而且可以

stop();
setTimeout(start, 2000);
Run Code Online (Sandbox Code Playgroud)

但这实际上并没有阻止测试.它确实似乎停两秒钟,但我不确定是什么.:-)

有任何想法吗?

epa*_*llo 21

你的测试看起来应该是这样的.

test('asynchronous test', function() {
    stop(); // Pause the test 
    //Add your wait
    setTimeout(function() {
       //Make assertion 
       ok(true);
       // After the assertion called, restart the test
       start();
    }, 1000);
});
Run Code Online (Sandbox Code Playgroud)

UPD: 在QUnit 2.x函数中,start()和stop()都消失了.建议assert.async()改用.更新后的代码如下:

    test('asynchronous test', function() {
        var done = assert.async();
        //Add your wait
        setTimeout(function() {
           //Make you assertion 
           ok(true);
           // Tell QUnit to wait for the done() call inside the timeout.
           done();
        }, 1000);
    });
Run Code Online (Sandbox Code Playgroud)


wut*_*utz 5

promise一旦元素的所有动画完成,您可以使用该函数触发回调.这意味着您需要知道在测试中运行动画的元素(但您不需要知道动画有多长).