[英]Asynchronous Javascript with Promises and unit/integration tests
我是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";
}
}
然后,为了确保我不会在途中破坏任何东西,我决定进行一些集成测试,所以我决定使用QUnit和qunit-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
您的getStatus
函数在任何异步调用解析之前返回结果。 你需要让getStatus
返回一个promise,当所有其他promises使用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);
});
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.