使用QUnit进行异步测试

时间:2017-01-12 06:31:16

标签: javascript jquery asynchronous qunit

代码:

var obj = {
    check: 0,
    foo: function() {
        ...
        jQuery.ajax({
            contentType : "application/json",
            type: "POST",
            async: true,
            ...,
            success: function(data) {
               check = 1;           
            },
            error: function(error) {
            }
        });
    }
}

测试代码:

    QUnit.test("Test asynchronus, function(assert) {
         obj.foo();
         assert.equal(obj.check, 1, "The check should be equal 1");
    });

使用当前代码,测试用例失败,因为断言在ajax的回调成功之前运行。

如何在不更改代码的情况下修复测试代码(例如:将async更改为false)。 assert.async是我问题的解决方案吗?

0 个答案:

没有答案
相关问题