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.
Best Answer
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