arempe93 / bunny-mock

A mock client for RabbitMQ modeled after the Bunny client in ruby
http://www.rubydoc.info/github/arempe93/bunny-mock
MIT License
44 stars 37 forks source link

Bunny Mock

Build Status Gem Version Coverage Status Documentation

A mock client for RabbitMQ, modeled after the popular Bunny client. It currently supports basic usage of Bunny for managing exchanges and queues, with the goal of being able to handle and test all Bunny use cases.

Upgrading

This project does its best to follow semantic versioning practices. Check the CHANGELOG to see detailed versioning notes, and UPGRADING for notes about major changes or deprecations.

Usage

BunnyMock can be injected into your RabbitMQ application in place of Bunny for testing. For example, if you have a helper module named AMQFactory, some code similar to the following placed in spec_helper or test_helper or what have you is all you need to start using BunnyMock to test your RabbitMQ application

require 'bunny-mock'

RSpec.configure do |config|
  config.before(:each) do
    AMQFactory.connection = BunnyMock.new.start
  end
end

For an example, easy to mock setup, check out this helper

Examples

Here are some examples showcasing what BunnyMock can do

Declaration

it 'should create queues and exchanges' do

  session = BunnyMock.new.start
  channel = session.channel

  queue = channel.queue 'queue.test'
  expect(session.queue_exists?('queue.test')).to be_truthy

  queue.delete
  expect(session.queue_exists?('queue.test')).to be_falsey

  xchg = channel.exchange 'xchg.test'
  expect(session.exchange_exists?('xchg.test')).to be_truthy

  xchg.delete
  expect(session.exchange_exists?('xchg.test')).to be_falsey
end

Publishing

it 'should publish messages to queues' do

  channel = BunnyMock.new.start.channel
  queue = channel.queue 'queue.test'

  queue.publish 'Testing message', priority: 5

  expect(queue.message_count).to eq(1)

  payload = queue.pop
  expect(queue.message_count).to eq(0)

  expect(payload[:message]).to eq('Testing message')
  expect(payload[:options][:priority]).to eq(5)
end

it 'should route messages from exchanges' do

  channel = BunnyMock.new.start.channel

  xchg = channel.topic 'xchg.topic'
  queue = channel.queue 'queue.test'

  queue.bind xchg, routing_key: '*.test'
  xchg.publish 'Routed message', routing_key: 'foo.test'

  expect(queue.message_count).to eq(1)
  expect(queue.pop[:message]).to eq('Routed message')
end

Binding

it 'should bind queues to exchanges' do

  channel = BunnyMock.new.start.channel

  queue = channel.queue 'queue.test'
  xchg = channel.exchange 'xchg.test'

  queue.bind xchg
  expect(queue.bound_to?(xchg)).to be_truthy
  expect(xchg.routes_to?(queue)).to be_truthy

  queue.unbind xchg
  expect(queue.bound_to?(xchg)).to be_falsey
  expect(xchg.routes_to?(queue)).to be_falsey

  queue.bind 'xchg.test'
  expect(queue.bound_to?(xchg)).to be_truthy
  expect(xchg.routes_to?(queue)).to be_truthy
end

it 'should bind exchanges to exchanges' do

  channel = BunnyMock.new.start.channel

  source = channel.exchange 'xchg.source'
  receiver = channel.exchange 'xchg.receiver'

  receiver.bind source
  expect(receiver.bound_to?(source)).to be_truthy
  expect(source.routes_to?(receiver)).to be_truthy

  receiver.unbind source
  expect(receiver.bound_to?(source)).to be_falsey
  expect(source.routes_to?(receiver)).to be_falsey

  receiver.bind 'xchg.source'
  expect(receiver.bound_to?(source)).to be_truthy
  expect(source.routes_to?(receiver)).to be_truthy
end

Other features

This gem was made based on my own use of Bunny in a project. If there are other uses for Bunny that this library does not cover (eg. missing methods, functionality), feel free to open an issue or pull request!

Installation

With RubyGems

To install BunnyMock with RubyGems:

gem install bunny-mock

With Bundler

To use BunnyMock with a Bundler managed project:

gem 'bunny-mock'

Documentation

View the documentation on RubyDoc

Dependencies

License

Released under the MIT license