Skip to content

Instantly share code, notes, and snippets.

@owen2345

owen2345/Gemfile

Created Aug 31, 2019
Embed
What would you like to do?
Multiple man subscribe testing
gem 'multiple_man'
gem 'bunny-mock'
RSpec.describe('Subscribe pub/sub events')
let(:service) { MultipleMan::Runner.new(mode: :general) }
let(:model) { build(:category) }
let(:model_name) { model.class.name }
before { service.run }
it "Create a new category when listen a new category event" do
push_data = { id: nil, name: 'Sample cat' }
service.publish_test_message(model_name, push_data, operation: :create)
expect(model.class.all.any?).to be_truthy
expect(model.class.last.name).to eq push_data[:name]
end
end
# disable amqp events for test env
RSpec.configure do |config|
config.before(:each) do
allow(Bunny).to receive(:new).and_return(BunnyMock.new)
end
end
# suppor/multiple_man_test.rb
# frozen_string_literal: true
# Prepare multiple man for testing
module MultipleMan
class Runner
attr_reader :listener, :queue
alias build_listener_old build_listener
# override method to save queue and fix exchange if not exist
def build_listener
topic_name = MultipleMan.configuration.topic_name
build_listener_old.tap do |listener|
@listener = listener
@queue = listener.send(:queue)
@queue.channel.exchange(topic_name)
end
end
# publish a test message using current connection
def publish_test_message(model_name, data, operation: :create, id: :id)
message = {
type: model_name,
data: data.merge(operation: operation)
}
message[:id] = { id => data[:id] }
routing_key = MultipleMan::RoutingKey.new(model_name, operation).to_s
queue.publish(message.to_json, routing_key: routing_key)
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment