1

I'm having trouble figuring how I would test a library that intended to be used for communicating with another web service. For example, I have a Messenger class that initializes a connection and requires a configuration and a remote address to run. How would I go about testing something like this?

import { MessengerConfig } from '../Interfaces/MessengerConfig';
import { Connection, ConfirmChannel, connect } from 'amqplib';
import * as Promise from 'bluebird';

export default class Messenger {

    logger;
    connection: Connection;
    channel: ConfirmChannel;
    config: MessengerConfig;

    constructor(logger, config: MessengerConfig) {
        this.logger = logger;
        this.connection = null;
        this.channel = null;
        this.config = config;
    }

    public start(durable: boolean = true): Promise<any> {
        return connect(this.config.host)
            .then((conn) => {
                this.connection = conn;
                return this.connection.createConfirmChannel();
            })
            .then((chan) => {
                this.channel = chan;
                return this.channel.prefetch(1);
            })
            .then(() => {
                return Promise.map(this.config.subKeys, (route) => {
                    return this.channel.assertExchange(route.exchange, 'direct', { durable: durable });
                });
            });
    }

}

What could I change to test the start function? Or is that even worth testing? In .NET this was a bit easier because I could create overloaded constructors to inject a mock HttpClient or whatever I needed. I can't overload functions in typescript/javascript like I would there, though.

4

Yeah seems a little pointless to mock all the library objects. You'd end up with a test which just tested your mock.

I would mock the rabbitmq server with a local one that returned dummy data and do an integration test

  • 1
    That's probably as good an approach as any, though to be fair, if you trust a library so little that you feel a suite of tests is required solely to test the library itself, then perhaps it's time to choose a different library. – Robert Harvey Jul 26 '17 at 23:36
  • well there is some stuff in there to test. like the route setup – Ewan Jul 27 '17 at 6:17
  • This seems to be the way I want to go. Need to figure out a way to mock a rabbitmq server in node though. Thanks! – Cuthbert Jul 28 '17 at 14:52
  • use a real one in a test env and inject a random queue name – Ewan Jul 28 '17 at 17:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.