【问题标题】:How to make QUnit wait for certain interval of time?如何让 QUnit 等待一定的时间间隔?
【发布时间】:2012-10-29 12:50:11
【问题描述】:
我在 QUnit 中定义了几个测试用例。这些测试用例中的最后一个使用 window.open 函数在新窗口中打开特定 URL,并将新打开页面的 body 标记的 html 内容附加到当前页面的(在其上运行测试) 身体标签。
此外,我需要在所有其他测试用例的执行结束时执行该测试用例。我可以通过将属性 QUnit.config.reorder 设置为 false 来做到这一点。
问题在于我需要等到新页面在新窗口中加载,然后才能复制正文 html(因为页面加载需要时间)。为此,我使用 setTimeout 在 3000 毫秒后获取 body 标记的 html 内容,但同时 QUnit 未能通过测试用例,因为断言存在于 setTimeOut 处理函数中。
【问题讨论】:
标签:
javascript
settimeout
qunit
【解决方案1】:
asyncTest('my test', function() {
// some prep. code goes here (may go here)
setTimeout(function() {
start(); // this would tell QUnit to start the test
// test code goes here
}, 3000);
});
【解决方案3】:
您可以将 Qunit 测试与异步断言一起使用,例如:
QUnit.test( "post an object for dup detection ", function( assert ) {
var done = assert.async();
$.ajax({
method : "POST",
url : ... ,
data : ... ,
dataType : "json"
}).done( function( data, textStatus, jqXHR){
assert.whatever();
done();
}).fail(function(jqXHR, textStatus, errorThrown){
assert.notOk();
console.log(errorThrown);
done();
});
});