Giter VIP home page Giter VIP logo

bunny-mock's Introduction

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

  • 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

License

Released under the MIT license

bunny-mock's People

Contributors

arempe93 avatar baelter avatar podung avatar syndbg avatar timothymdean avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.