Created
August 31, 2019 16:51
-
-
Save owen2345/6990e7d1b6107df3fb7f497e9ef2db7e to your computer and use it in GitHub Desktop.
Multiple man subscribe testing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
gem 'multiple_man' | |
gem 'bunny-mock' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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