vendredi 14 décembre 2018

How to test a promise that connects to RabbitMQ?

Hi I'm trying to test a custom function that connects to RabbitMQ passing a wrong host:

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

If the connection fail I throw a Error, so I'm testing it like this:

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

The connection fails and throws a error but my test is not testing that, simply I got the exception on my terminal:

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.

I tried catching the exception on a trycatch block but it's the same issue.

Aucun commentaire:

Enregistrer un commentaire