如何告诉QUnit将asyncTest期间的错误视为测试失败并继续进行下一个测试?
下面是一个在ReferenceError之后QUnit停止运行的例子:jsfiddle
发布于 2013-10-08 11:29:31
如果在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/
(编辑:按评论更新。)
https://stackoverflow.com/questions/19238530
复制相似问题