mercredi 23 décembre 2015

Testing function calls that depend on an object returned by a callback

I would like to test the following code:

'use strict';

const amqp = require('amqplib');
const Promise = require('bluebird');

const queueManager = function queueManager() {
  const amqp_host = 'amqp://' + process.env.AMQP_HOST || 'localhost';

  return {
    setupQueue: Promise.method(function setupQueue(queue) {
      return amqp.connect(amqp_host)
        .then(conn => conn.createConfirmChannel())
        .tap(channel => channel.assertQueue(queue));
    }),
    enqueueJob: Promise.method(function enqueueJob(channel, queue, job) {
      return channel.sendToQueue(queue, new Buffer(job));
    }),
    consumeJob: Promise.method(function consumeJob(channel, queue) {
      return channel.consume(queue, msg => msg);
    })
  };
};

module.exports = {
  create: queueManager
}

I want to test my setupQueue, enqueueJob and consumeJob methods to make sure they do the right things to the AMQP server.

For setupQueue for instance, I want to make sure it uses Channel.createConfirmChannel instead of say Channel.createChannel and that it also does Channel.assertQueue.

However, I don't know how to do that.

If I mock the amqp variable with proxyquire, all I'll be able to spy on is the amqp.connect call. I'll probably stub it to avoid hitting any AMQP servers. But what about the following statements? How do I tap into the conn and channel objects?

Aucun commentaire:

Enregistrer un commentaire