how can you mock functions from libraries like amqpdial?

Given the following types:

type AmqpChannel interface {
    ExchangeDeclare(name, kind string, durable, autoDelete, internal, noWait bool, args amqp.Table) error
    QueueDeclare(name string, durable, autoDelete, exclusive, noWait bool, args amqp.Table) (amqp.Queue, error)
    QueueBind(name, key, exchange string, noWait bool, args amqp.Table) error
    Consume(queue, consumer string, autoAck, exclusive, noLocal, noWait bool, args amqp.Table) (<-chan amqp.Delivery, error)
    Publish(exchange, key string, mandatory, immediate bool, msg amqp.Publishing) error

type AmqpConnection interface {
    Channel() (AmqpChannel, error)
    Close() error

type AmqpDial func(url string) (AmqpConnection, error)

You can create simple wrappers that delegate to the actual code:

func AmqpDialWrapper(url string) (AmqpConnection, error) {
    conn, err := amqp.Dial(url)
    if err != nil {
        return nil, err
    return AmqpConnectionWrapper{conn}, nil

type AmqpConnectionWrapper struct {
    conn *amqp.Connection

// If *amqp.Channel does not satisfy the consumer.AmqpChannel interface
// then you'll need another wrapper, a AmqpChannelWrapper, that implements
// the consumer.AmqpChannel interface and delegates to *amqp.Channel.
func (w AmqpConnectionWrapper) Channel() (AmqpChannel, error) {
    return w.conn.Channel()

func (w AmqpConnectionWrapper) Close() error {
    return w.conn.Close()

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top