如何告诉QUnit在asyncTest
期间将错误视为测试失败并继续下一次测试?
这是QUnit在ReferenceError
之后停止运行的示例:jsfiddle
答案 0 :(得分:1)
异步测试中的错误如果在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/
(修改:根据评论更新。)