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.
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.
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
Here are some examples showcasing what BunnyMock can do
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
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
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
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!
To install BunnyMock with RubyGems:
gem install bunny-mock
To use BunnyMock with a Bundler managed project:
gem 'bunny-mock'
View the documentation on RubyDoc
Bunny - To use original exception classes
Ruby version >= 2.0 (A requirement of Bunny) Now works with other Ruby versions (even JRuby!) thanks to @TimothyMDean
Released under the MIT license