Add the library to the test section in your application's Gemfile:
group :test do
gem 'testcontainers-rabbitmq'
end
And then execute:
$ bundle install
Or install it yourself as:
$ gem install testcontainers-rabbitmq
To use the library, you first need to require it:
require "testcontainers/rabbitmq"
Create a new instance of the Testcontainers::RabbitmqContainer
class:
container = Testcontainers::RabbitmqContainer.new
This creates a new container with the default RabbitMQ image, user, password, and vhost. You can customize these by passing arguments to the constructor:
container = Testcontainers::RabbitmqContainer.new("rabbitmq:latest", username: "custom_user", password: "custom_pass", vhost: "custom_vhost")
Start the container:
container.start
Stop the container when you're done:
container.stop
Once the container is running, you can obtain the connection details using the following methods:
host = container.host
port = container.first_mapped_port
Or, you can generate a full RabbitMQ URL:
broker_url = container.rabbitmq_url
container.with_vhost("custom_vhost")
container.with_username("custom_user")
container.with_password("custom_pass")
There are complete examples of how to use testcontainers-rabbitmq to create containers, connects to it, publish and consume simple message:
require "testcontainers/rabbitmq"
require "bunny"
container = Testcontainers::RabbitmqContainer.new
container.start
connection = Bunny.new(container.rabbitmq_url)
connection.start
channel = connection.create_channel
queue = channel.queue('hello')
channel.default_exchange.publish('Hello World!', routing_key: queue.name)
connection.close
The previous example creates a RabbitMQ container, connects to it using bunny
gem, then publish a message to a queue named hello
require "testcontainers/rabbitmq"
require "bunny"
container = Testcontainers::RabbitmqContainer.new
container.start
connection = Bunny.new(container.rabbitmq_url)
connection.start
channel = connection.create_channel
queue = channel.queue('hello')
queue.subscribe(block: true) do |_delivery_info, _properties, body|
puts " [x] Received #{body}"
end
connection.close
The previous example creates a RabbitMQ container, connects to it using bunny
gem, then reads messages from a queue named hello
Bug reports and pull requests are welcome on GitHub at https://github.com/testcontainers/testcontainers-ruby. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the code of conduct.
The gem is available as open source under the terms of the MIT License.
Everyone interacting in the Testcontainers project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the code of conduct.