如何测试连接到RabbitMQ的Prom?

时间:2018-12-15 00:31:11

标签: javascript unit-testing testing

嗨,我正在使用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块上捕获异常,但这是相同的问题。

编辑:将测试更改为以下内容后,我在终端上收到此错误:

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)

4 个答案:

答案 0 :(得分:1)

您没有正确失败。您忘记了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可能会帮助您正确地测试承诺。

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

答案 1 :(得分:1)

您需要reject这个承诺。

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

和测试

it('shouldnt connect to RabbitMQ service successfully with the wrong host.', () => {
  return rabbitmqmailer.connect('amqp://wronghost')
    .then(() => { assert.fail('was not supposed to succeed'); })
    .catch((err) => { expect(err).to.be.an.instanceof(Error); })
})

答案 2 :(得分:0)

您通常希望隔离测试。我建议模拟您的amqp对象,并期望调用connect方法。

在设计模拟程序之前,您必须尝试并理解真正的connect方法很重要

您可以使用https://sinonjs.org/

这样的模拟框架

答案 3 :(得分:0)

最后我找到了测试的方法

it('shouldnt connect to RabbitMQ service successfully with the wrong host.', (done) => {
      (async () => {
        try {
          await rabbitmqmailer.connect('amqp://wronghost');
        } catch (error) {
          chai.assert.typeOf(error, 'error');
        } finally {
          done();
        }
      })();
    });

但是有一个奇怪的问题,例如,如果我将类型更改为“字符串”,它会告诉我:

(node:26053) UnhandledPromiseRejectionWarning: AssertionError: expected [Error: Error: getaddrinfo EAI_AGAIN wronghost wronghost:5672] to be a string

但是!测试成功通过:

 ✓ shouldnt connect to RabbitMQ service successfully with the wrong host.

我不知道为什么会发生这种情况,但是无论如何它都会起作用,谢谢您的帮助。