如何告诉QUnit在asyncTest
期间将错误视为测试失败并继续进行下一个测试?QUYNit asyncTest在错误后不会继续
这里是QUnit停止一个ReferenceError
后运行一个例子:jsfiddle
如何告诉QUnit在asyncTest
期间将错误视为测试失败并继续进行下一个测试?QUYNit asyncTest在错误后不会继续
这里是QUnit停止一个ReferenceError
后运行一个例子:jsfiddle
错误异步测试中默默死去,如果出现而QUnit没有正式运行他们。
最简单的解决方案是将每个asyncTest
内容封装在try/catch块中,该块在传播任何错误之后重新启动QUnit。实际上,我们实际上不得不污染具有一百万次尝试/捕获的代码 - 我们可以自动装饰您现有的方法。
例如:
// surrounds any function with a try/catch block to propagate errors to QUnit when
// called during an asyncTest
function asyncTrier(method) {
return function() {
try{
// if the method runs normally, great!
method();
} catch (e) {
// if not, restart QUnit and pass the error on
QUnit.start();
throw new (e);
}
};
}
QUnit.asyncTest("sample", 1, function() {
setTimeout(asyncTrier(function(){
var foo = window.nonexistentobj.toString() + ""; // throws error
QUnit.ok("foo defined", !!foo)
QUnit.start();
}), 1000);
});
分叉的小提琴,与样品包装方法自动应用这样一个try/catch周围的每一个异步块:http://jsfiddle.net/bnMWd/4/
(编辑:每评论更新。 )
我可以使用'testTimeouts',但它有风险,因为测试可能需要很多时间,并且如果您有一些错误测试,它将花费很多时间来运行所有测试! – Bakhshi