gpt4 book ai didi

javascript - 如何测试连接到 RabbitMQ 的 promise ?

转载 作者:行者123 更新时间:2023-11-28 20:26:37 25 4
gpt4 key购买 nike

您好,我正在使用 Chai 并尝试测试连接到传递错误主机的 RabbitMQ 的自定义函数:

connect(host) {
return new Promise((resolve, reject) => {
amqp.connect(host)
.then((conn) => {
resolve(conn);
})
.catch((err) => {
throw new Error(err);
});
});
}

如果连接失败,我会抛出一个错误,所以我会这样测试它:

it('shouldnt connect to RabbitMQ service successfully with the wrong host.', async () => {
const result = await rabbitmqmailer.connect('amqp://wronghost');
expect(result).to.equal(Error);
});

连接失败并抛出错误,但我的测试并未对此进行测试,只是我的终端出现了异常:

RabbitMQMailer component.
RabbitMQMailer configuration information.
✓ should test rabbitmqmailer host configuration.
✓ should test rabbitmqmailer queue configuration.
✓ should get rabbitmqmailer empty emailContent value after make a new instance.
✓ should get rabbitmqmailer empty emailContentConsumed value after make a new instance.
✓ resolves
✓ should connect to RabbitMQ service successfully with the correct host. (60ms)
Unhandled rejection Error: Error: getaddrinfo EAI_AGAIN wronghost wronghost:5672
at _amqplib2.default.connect.then.catch.err (/home/ubuntu/Desktop/easy-tracking/backend/src/components/rabbitmqmailer/rabbitmqmailer.dal.js:1:11069)
at tryCatcher (/home/ubuntu/Desktop/easy-tracking/backend/node_modules/amqplib/node_modules/bluebird/js/release/util.js:16:23)
at Promise._settlePromiseFromHandler (/home/ubuntu/Desktop/easy-tracking/backend/node_modules/amqplib/node_modules/bluebird/js/release/promise.js:512:31)
at Promise._settlePromise (/home/ubuntu/Desktop/easy-tracking/backend/node_modules/amqplib/node_modules/bluebird/js/release/promise.js:569:18)
at Promise._settlePromise0 (/home/ubuntu/Desktop/easy-tracking/backend/node_modules/amqplib/node_modules/bluebird/js/release/promise.js:614:10)
at Promise._settlePromises (/home/ubuntu/Desktop/easy-tracking/backend/node_modules/amqplib/node_modules/bluebird/js/release/promise.js:690:18)
at _drainQueueStep (/home/ubuntu/Desktop/easy-tracking/backend/node_modules/amqplib/node_modules/bluebird/js/release/async.js:138:12)
at _drainQueue (/home/ubuntu/Desktop/easy-tracking/backend/node_modules/amqplib/node_modules/bluebird/js/release/async.js:131:9)
at Async._drainQueues (/home/ubuntu/Desktop/easy-tracking/backend/node_modules/amqplib/node_modules/bluebird/js/release/async.js:147:5)
at Immediate.Async.drainQueues [as _onImmediate] (/home/ubuntu/Desktop/easy-tracking/backend/node_modules/amqplib/node_modules/bluebird/js/release/async.js:17:14)
at processImmediate (timers.js:632:19)

1) shouldnt connect to RabbitMQ service successfully with the wrong host.

我尝试在 trycatch block 上捕获异常,但这是同一个问题。

编辑:将我的测试更改为:

it('shouldnt connect to RabbitMQ service successfully with the wrong host.', async (done) => {
const result = await rabbitmqmailer.connect('amqp://wronghost');
expect(result).to.be.an.instanceof(Error);
done();
});


(node:18911) UnhandledPromiseRejectionWarning: Error: Error: getaddrinfo EAI_AGAIN wronghost wronghost:5672
at _amqplib2.default.connect.then.catch.err (/home/ubuntu/Desktop/easy-tracking/backend/src/components/rabbitmqmailer/rabbitmqmailer.dal.js:1:11475)
at tryCatcher (/home/ubuntu/Desktop/easy-tracking/backend/node_modules/amqplib/node_modules/bluebird/js/release/util.js:16:23)
at Promise._settlePromiseFromHandler (/home/ubuntu/Desktop/easy-tracking/backend/node_modules/amqplib/node_modules/bluebird/js/release/promise.js:512:31)
at Promise._settlePromise (/home/ubuntu/Desktop/easy-tracking/backend/node_modules/amqplib/node_modules/bluebird/js/release/promise.js:569:18)
at Promise._settlePromise0 (/home/ubuntu/Desktop/easy-tracking/backend/node_modules/amqplib/node_modules/bluebird/js/release/promise.js:614:10)
at Promise._settlePromises (/home/ubuntu/Desktop/easy-tracking/backend/node_modules/amqplib/node_modules/bluebird/js/release/promise.js:690:18)
at _drainQueueStep (/home/ubuntu/Desktop/easy-tracking/backend/node_modules/amqplib/node_modules/bluebird/js/release/async.js:138:12)
at _drainQueue (/home/ubuntu/Desktop/easy-tracking/backend/node_modules/amqplib/node_modules/bluebird/js/release/async.js:131:9)
at Async._drainQueues (/home/ubuntu/Desktop/easy-tracking/backend/node_modules/amqplib/node_modules/bluebird/js/release/async.js:147:5)
at Immediate.Async.drainQueues (/home/ubuntu/Desktop/easy-tracking/backend/node_modules/amqplib/node_modules/bluebird/js/release/async.js:17:14)
at processImmediate (timers.js:632:19)
(node:18911) 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(). (rejection id: 2)
(node:18911) [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.
1) shouldnt connect to RabbitMQ service successfully with the wrong host.


1) RabbitMQMailer component.
RabbitMQMailer configuration information.
shouldnt connect to RabbitMQ service successfully with the wrong host.:
Error: Timeout of 2000ms exceeded. For async tests and hooks, ensure "done()" is called; if returning a Promise, ensure it resolves. (/home/ubuntu/Desktop/easy-tracking/backend/src/components/rabbitmqmailer/rabbitmqmailer.test.js)

最佳答案

你没有正确地失败。您忘记了 reject。改为这样做:

connect(host) {
return new Promise((resolve, reject) => {
amqp.connect(host)
.then((conn) => {
resolve(conn);
})
.catch((err) => {
reject(new Error(err)); // Pass the error to reject
});
});
}

在您的测试中,使用 instanceof匹配返回的 Error:

it('shouldnt connect to RabbitMQ service successfully with the wrong host.', async () => {
const result = await rabbitmqmailer.connect('amqp://wronghost');
expect(result).to.be.an.instanceof(Error);
});

我也不知道你用什么来测试,但如果它是jest,那么这个link可能会帮助您正确测试 promise 。

编辑:实际上是 nvm。我看到你在使用 Chai

关于javascript - 如何测试连接到 RabbitMQ 的 promise ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53788525/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com