具有Promises和单元/集成测试的异步Javascript

时间:2017-01-17 23:46:02

标签: javascript unit-testing asynchronous

我是Javascript和异步世界的新手,似乎两者都令我头疼。

所以,我有第三方客户端API,其中包含以下async方法getItem(id)

现在我正在尝试根据子项的子项确定父项的状态:

getStatus = function(ids) {
    var failedCount = 0;
    var successCount = 0;
    var warningCount = 0;

    ids.forEach(function(id) {
        //calling the async function
        getItem(id).then(function(item) {
            var state = item.State;
            if(state == "Success") {
                successCount++;
            } else if(state == "Failed") {
                failedCount++;
            } else if(state == "Warning") {
                warningCount++;
            }
        });
    });
    if(failedCounter > 0) {
        return "Failed";
    } else if(warningCounter > 0) {
        return "Warning";
    } else if(successCounter == ids.length) {
        return "Success";
    } else {
        return "Not run yet";
    }
}

然后,为了确保我不会在途中破坏任何东西,我决定进行一些集成测试,所以我选择了QUnitqunit-parameterize

QUnit.cases([
    {
        title : "Success, Failed => Failed",
        ids : [1,2],
        expectedItemStateAfter : "Failed"
    }
    ]).test( "", function( params, assert ) {
          var done = assert.async(2);
          setTimeout(function() {
             var value = getStatus(params.ids);       
             assert.equal(value, params.expectedItemStateAfter);
             done();
            }, 2000);
});

尝试调整setTimeout超时,尝试使用assert.async(2)assert.async();,每个QUnit的默认值,但无效,最终结果仍然是每次都相同即使经过一堆阅读并试图理解我也不知道我做错了什么:

1. failed     @ 2004 ms
Expected: "Failed"
Result:   undefined
Diff:   "Failed" undefined 

1 个答案:

答案 0 :(得分:4)

您的getStatus函数在任何异步调用解析之前返回结果。您需要让getStatus返回一个承诺,当所有其他承诺使用Promise.all解决时,该承诺会解决:

var getStatus = function(ids) {
    var failedCount = 0;
    var successCount = 0;
    var warningCount = 0;

    return Promise.all(ids.map(function(id) {
        return getItem(id).then(function(item) {
            var state = item.State;
            if (state === "Success") {
                successCount++;
            } else if (state === "Failed") {
                failedCount++;
            } else if (state === "Warning") {
                warningCount++;
            }
        });
    })).then(function() {
        if (failedCounter > 0) {
            return "Failed";
        } else if (warningCounter > 0) {
            return "Warning";
        } else if (successCounter === ids.length) {
            return "Success";
        } else {
            return "Not run yet";
        }
    });
};

然后,您需要调整测试代码,以便它使用promise而不是setTimeout。请注意,您可以从QUnit.test回调中返回一个可对象的对象,它会自动处理Promise的解析。

QUnit.cases([
    { title: "Success, Failed => Failed", ids: [1, 2], expectedStatus: "Failed" }
]).test("getStatus() test", function(params, assert) {
    return getStatus(params.ids).then(function(value) {
        assert.equal(value, params.expectedStatus);
    });
});