我正在使用CodeceptJS编写测试以检查事务是否已成功注册:
const { I, signInPage, helpers } = inject();
const util = require('util');
var Request = require("request");
const requestPromise = util.promisify(Request);
function sleep(seconds)
{
return new Promise((resolve) => {
setTimeout(resolve, seconds * 1000);
});
}
async function getNumberOfTransactions()
{
var result = await requestPromise('http://my.service.com/trans_count.php?user_id=12345');
return JSON.parse(result.body).transactions;
}
Scenario('My Test', async () =>
{
I.amOnPage('some-page');
// ...
// Perform an online transaction...
// ...
I.see('Transaction successful');
var attempts = 0;
var current_transaction_count = await getNumberOfTransactions();
while (current_transaction_count < expected_transaction_count && attempts < 60) {
await sleep(60);
I.say('Current transaction count: ' + current_transaction_count + ', expected transaction count: ' + expected_transaction_count);
current_transaction_count = await getNumberOfTransactions();
attempts++;
}
I.assertEqual(
expected_transaction_count,
current_transaction_count,
"********** Transaction not tracked after one hour **********"
);
}
我的问题是,当页面上没有“交易成功”时,行I.see('Transaction successful');
不会停止脚本执行。相反,我在输出中得到了这个
(node:48370) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 1)
(node:48370) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
我不精通JavaScript / NodeJS,因此请在“入门”中进行解释:)