【发布时间】:2018-06-03 07:12:48
【问题描述】:
我正在使用 mocha 为我的 Node JS 应用程序编写测试
在我的测试中,我模拟了 1 个函数(它正在调用 HTTP Url),并且在某些情况下,我添加了 1 秒的睡眠时间。
由于 mocha 测试中的睡眠功能,我正在低于异常。在实际应用中它工作正常。
错误:异步挂钩堆栈已损坏(实际:18,预期: 19) 1: v8::SnapshotCreator::default 构造函数闭包 2:
node::CallbackScope::~CallbackScope 3:
node::CallbackScope::~CallbackScope 4: RAND_query_egd_bytes 5:
RAND_query_egd_bytes 6:uv_timer_get_repeat 7:uv_run 8:
000007FEF8771261 9:000007FEF87710B6 10:
v8::internal::wasm::SignatureMap::Find 11:
v8::internal::Builtins::CallableFor 12:
v8::internal::Builtins::CallableFor 13:
v8::internal::Builtins::CallableFor 14: 000002E6363043C1
下面是我的moched函数代码。
somefunction() //In Mock test
{
let current_time = Math.round((new Date()).getTime() / 1000);
if (last_execution_time == current_time) {
admin_delete_user_count++;
if (admin_delete_user_count >= 3) {
callback({
stack: "TooManyRequestsException: Rate exceeded",
"code": "TooManyRequestsException",
"statusCode": 400
}, undefined);
return;
}
} else {
admin_delete_user_count = 0;
last_execution_time = Math.round((new Date()).getTime() / 1000);
}
callback(undefined, "Test");
}
以下是我的节点 js 应用程序中的实际功能,它在我从上面的代码发送异常后导致问题。
somefunction() // In Real applicatio
{
if (err) {
if (err.code == "TooManyRequestsException") {
logger.log("INFO", "TooManyRequestsException, So wait a while for 1 second and retry");
index = index - 1;
sleep(1000); // THIS IS CAUSING THE ISSUE
}
} else {
console.log("deletedUsers:" + JSON.stringify(deletedUsers));
}
}
有什么帮助吗?
【问题讨论】:
标签: node.js unit-testing mocha.js