2011-11-18 159 views
15

我有jQuery代码,当我点击一个链接它首先皮上,然后删除一些HTML,像这样:即确保在QUnit等待测试

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

我想打一个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"); 

问题是隐藏动画已经运行结束之前确定()运行测试,当然,这样的offenting HTML尚未拆除,并导致测试失败。

我试过各种方式延迟/停止测试一秒左右,但似乎没有工作。最明显的一个就是用asynTest做

stop(); 
setTimeout(start, 2000); 

但是这并没有真正阻止测试。它似乎停止东西两秒钟,但我不知道是什么。 :-)

任何想法?

回答

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

宾果!这是事情的正确顺序。 :-)所以stop()实际上只停止了测试的结束? –

+1

此方法已被弃用(http://qunitjs.com/upgrade-guide-2.x/#replace-stop-and-start-with-assert-async)。 @ ekcrisp的答案使用了新的方法。 – Joe

+0

6年前提出的问题已被弃用,这令人感到惊讶。大声笑 – epascarello

5

您可以使用promise功能火一次一个元素的所有动画完成回调。这意味着您需要知道测试中运行动画的元素(但不需要知道动画有多长时间)。

+0

这是一个有趣的想法,但我暂时停留在jQuery 1.4上,所以没有办法。 –

4

使用QUnit断言对象,你可以做

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

});