等待QUnit测试
我有jQuery代码,当我点击它首先隐藏的链接然后删除一些HTML,如下所示:
$(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");
问题当然是在隐藏动画运行结束之前运行ok()测试,因此尚未删除有问题的HTML,并且测试失败。
我已经尝试了各种方法来延迟/停止测试一秒左右,但似乎没有任何工作。 最明显的一个是使用asynTest而且可以
stop(); setTimeout(start, 2000);
但这实际上并没有阻止测试。 它确实似乎停了两秒钟,但我不确定是什么。 🙂
有任何想法吗?
你的测试看起来应该是这样的。
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); });
一旦元素的所有动画完成,您可以使用promise
函数触发回调。 这意味着您需要知道动画在测试中运行的元素(但您不需要知道动画的长度)。
- 示例: http : //jsfiddle.net/4RqaA/1/
- Doc在这里: http : //api.jquery.com/promise/
您可以使用QUnit断言对象
test("async test", function (assert) { var done = assert.async(); setTimeout(function() { delayedPartOfTest(); done(); }, 20000); }) });