简体   繁体   English

等待QUnit测试

[英]Waiting in QUnit tests

I have jQuery code that when I click on a link it first hides and then removes some HTML, like so: 我有jQuery代码,当我点击它首先隐藏的链接然后删除一些HTML,如下所示:

$(this).parent().parent().hide('slow', function () {
    $(this).remove();
});

I want to make a QUnit test that makes sure that the HTML in question was deleted: 我想进行一个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");

The problem is of course that the ok() test is run before the hide animation has run to an end, so the offenting HTML hasn't been removed yet, and the test fails. 问题当然是在隐藏动画运行结束之前运行ok()测试,因此尚未删除有问题的HTML,并且测试失败。

I've tried various ways of delaying/stopping the test for a second or so, but nothing seems to work. 我已经尝试了各种方法来延迟/停止测试一秒左右,但似乎没有任何工作。 The most obvious one is to use an asynTest and do 最明显的一个是使用asynTest而且可以

stop();
setTimeout(start, 2000);

But this doesn't actually stop the test. 但这实际上并没有阻止测试。 It does seem to stop something for two seconds, but I'm not sure what. 它确实似乎停两秒钟,但我不确定是什么。 :-) :-)

Any ideas? 有任何想法吗?

Your test should look something like this. 你的测试看起来应该是这样的。

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);
});

UPD: In QUnit 2.x functions start() and stop() are gone . UPD: 在QUnit 2.x函数中,start()和stop()都消失了 It is recommended to use assert.async() instead. 建议使用assert.async()代替。 Updated code looks like: 更新后的代码如下:

    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);
    });

You could use the promise function to fire a callback once all animations for an element are finished. 一旦元素的所有动画完成,您可以使用promise函数触发回调。 This implies that you need to know on what elements the animations are run on in the test (but you don't need to know how long the animation is). 这意味着您需要知道在测试中运行动画的元素(但您不需要知道动画有多长)。

using the QUnit assert object you can do 您可以使用QUnit断言对象

test("async test", function (assert) {
    var done = assert.async();
    setTimeout(function() {
            delayedPartOfTest();
            done();
        }, 20000);
    })

});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM