错误后,QUnit asyncTest不会继续 [英] QUnit asyncTest does not continue after error

查看:105
本文介绍了错误后,QUnit asyncTest不会继续的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何告诉QUnit在 asyncTest 期间将错误视为测试失败并继续进行下一次测试?

How can I tell QUnit to consider errors during asyncTest as test failures and continue to next test?

这是一个示例,在 ReferenceError 之后QUnit停止运行: jsfiddle

here is an example which QUnit stops running after a ReferenceError: jsfiddle

推荐答案

如果在QUnit未正式运行时出现异步测试中的错误,则会出现静默。

Errors in asynchronous tests die silently if they arise while QUnit isn't officially running.

最简单的解决方案是将每个 asyncTest 内容包装在try / catch块中,该块在 >重新启动QUnit。我们实际上不需要用一百万次尝试/捕获来污染代码 - 我们可以自动装饰你现有的方法。

The simplest solution is to wrap every asyncTest contents in a try/catch block that propagates any errors after restarting QUnit. We don't actually have to pollute the code with a million try/catches--we can decorate your existing methods automagically.

例如:

// 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/

Forked your Fiddle, with a sample wrapping method to automatically apply such a try/catch around every asynchronous block: http://jsfiddle.net/bnMWd/4/

修改:根据评论更新。)

这篇关于错误后,QUnit asyncTest不会继续的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆