spec/units/message_driver/client_spec.rb in message-driver-0.4.0 vs spec/units/message_driver/client_spec.rb in message-driver-0.5.0

- old
+ new

@@ -1,11 +1,11 @@ require 'spec_helper' require 'message_driver/adapters/in_memory_adapter' module MessageDriver - describe Client do + RSpec.describe Client do class TestPublisher include Client end let(:logger) { MessageDriver.logger } @@ -39,218 +39,218 @@ end end end context 'with a given adapter_context' do - around(:each) do |example| + around(:example) do |example| subject.with_adapter_context(adapter_context, &example) end describe '#dynamic_destination' do let(:dest_name) { 'my_new_queue' } - let(:dest_options) { {type: 2} } - let(:message_props) { {expires: 'soon'} } + let(:dest_options) { { type: 2 } } + let(:message_props) { { expires: 'soon' } } let(:created_dest) { double('created destination') } before do - adapter_context.stub(:create_destination) { created_dest } + allow(adapter_context).to receive(:create_destination) { created_dest } end it 'delegates to the adapter_context' do result = subject.dynamic_destination(dest_name, dest_options, message_props) expect(result).to be(created_dest) - adapter_context.should have_received(:create_destination).with(dest_name, dest_options, message_props) + expect(adapter_context).to have_received(:create_destination).with(dest_name, dest_options, message_props) end it 'only requires destination name' do result = subject.dynamic_destination(dest_name) expect(result).to be(created_dest) - adapter_context.should have_received(:create_destination).with(dest_name, {}, {}) + expect(adapter_context).to have_received(:create_destination).with(dest_name, {}, {}) end end describe '#publish' do let(:destination) { broker.destination(:my_queue, 'my_queue', exclusive: true) } let(:body) { 'my message' } - let(:headers) { {foo: :bar} } - let(:properties) { {bar: :baz} } + let(:headers) { { foo: :bar } } + let(:properties) { { bar: :baz } } before do - adapter_context.stub(:publish) + allow(destination).to receive(:publish) end - it 'delegates to the adapter_context' do + it 'delegates to the destination' do subject.publish(destination, body, headers, properties) - adapter_context.should have_received(:publish).with(destination, body, headers, properties) + expect(destination).to have_received(:publish).with(body, headers, properties) end - it 'only requires destination and body' do + it 'only requires the body' do subject.publish(destination, body) - adapter_context.should have_received(:publish).with(destination, body, {}, {}) + expect(destination).to have_received(:publish).with(body, {}, {}) end it 'looks up the destination if necessary' do destination subject.publish(:my_queue, body, headers, properties) - adapter_context.should have_received(:publish).with(destination, body, headers, properties) + expect(destination).to have_received(:publish).with(body, headers, properties) end context "when the destination can't be found" do let(:bad_dest_name) { :not_a_queue } it 'raises a MessageDriver:NoSuchDestinationError' do - expect { + expect do subject.publish(bad_dest_name, body, headers, properties) - }.to raise_error(MessageDriver::NoSuchDestinationError, /#{bad_dest_name}/) - adapter_context.should_not have_received(:publish) + end.to raise_error(MessageDriver::NoSuchDestinationError, /#{bad_dest_name}/) + expect(destination).not_to have_received(:publish) end end end describe '#pop_message' do let(:expected) { double(MessageDriver::Message) } let(:destination) { broker.destination(:my_queue, 'my_queue', exclusive: true) } - let(:options) { {foo: :bar} } + let(:options) { { foo: :bar } } before do - adapter_context.stub(:pop_message) + allow(destination).to receive(:pop_message) end it 'delegates to the adapter_context' do subject.pop_message(destination, options) - adapter_context.should have_received(:pop_message).with(destination, options) + expect(destination).to have_received(:pop_message).with(options) end it 'looks up the destination if necessary' do destination subject.pop_message(:my_queue, options) - adapter_context.should have_received(:pop_message).with(destination, options) + expect(destination).to have_received(:pop_message).with(options) end context "when the destination can't be found" do let(:bad_dest_name) { :not_a_queue } it 'raises a MessageDriver:NoSuchDestinationError' do - expect { + expect do subject.pop_message(bad_dest_name, options) - }.to raise_error(MessageDriver::NoSuchDestinationError, /#{bad_dest_name}/) - adapter_context.should_not have_received(:pop_message) + end.to raise_error(MessageDriver::NoSuchDestinationError, /#{bad_dest_name}/) + expect(destination).not_to have_received(:pop_message) end end - it 'requires the destination and returns the message' do - adapter_context.should_receive(:pop_message).with(destination, {}).and_return(expected) + it 'requires just the destination and returns the message' do + expect(destination).to receive(:pop_message).with({}).and_return(expected) actual = subject.pop_message(destination) expect(actual).to be expected end it 'passes the options through and returns the message' do - adapter_context.should_receive(:pop_message).with(destination, options).and_return(expected) + expect(destination).to receive(:pop_message).with(options).and_return(expected) actual = subject.pop_message(destination, options) expect(actual).to be expected end end describe '#with_message_transaction' do before do - adapter_context.stub(:begin_transaction) - adapter_context.stub(:commit_transaction) - adapter_context.stub(:rollback_transaction) + allow(adapter_context).to receive(:begin_transaction) + allow(adapter_context).to receive(:commit_transaction) + allow(adapter_context).to receive(:rollback_transaction) end context 'when the adapter supports transactions' do before do - adapter_context.stub(:supports_transactions?) { true } + allow(adapter_context).to receive(:supports_transactions?) { true } end it 'delegates to the adapter context' do - expect { |blk| + expect do |blk| subject.with_message_transaction(&blk) - }.to yield_control - adapter_context.should have_received(:begin_transaction) - adapter_context.should have_received(:commit_transaction) + end.to yield_control + expect(adapter_context).to have_received(:begin_transaction) + expect(adapter_context).to have_received(:commit_transaction) end context 'when the block raises an error' do it 'calls rollback instead of commit and raises the error' do - expect { + expect do subject.with_message_transaction do - raise 'having a tough time' + fail 'having a tough time' end - }.to raise_error 'having a tough time' - adapter_context.should have_received(:begin_transaction) - adapter_context.should_not have_received(:commit_transaction) - adapter_context.should have_received(:rollback_transaction) + end.to raise_error 'having a tough time' + expect(adapter_context).to have_received(:begin_transaction) + expect(adapter_context).not_to have_received(:commit_transaction) + expect(adapter_context).to have_received(:rollback_transaction) end context 'and the the rollback raises an error' do it 'logs the error from the rollback and raises the original error' do allow(logger).to receive(:error) - adapter_context.stub(:rollback_transaction).and_raise('rollback failed!') - expect { + allow(adapter_context).to receive(:rollback_transaction).and_raise('rollback failed!') + expect do subject.with_message_transaction do - raise 'having a tough time' + fail 'having a tough time' end - }.to raise_error 'having a tough time' + end.to raise_error 'having a tough time' expect(logger).to have_received(:error).with(match('rollback failed!')) end end end context 'when the transactions are nested' do it 'only starts and commits once' do - expect { |blk| + expect do |blk| subject.with_message_transaction do subject.with_message_transaction(&blk) end - }.to yield_control - adapter_context.should have_received(:begin_transaction).once - adapter_context.should have_received(:commit_transaction).once + end.to yield_control + expect(adapter_context).to have_received(:begin_transaction).once + expect(adapter_context).to have_received(:commit_transaction).once end context 'when the block raises an error' do it 'calls rollback instead of commit and raises the error' do - expect { + expect do subject.with_message_transaction do subject.with_message_transaction do - raise 'having a tough time' + fail 'having a tough time' end end - }.to raise_error 'having a tough time' - adapter_context.should have_received(:begin_transaction).once - adapter_context.should_not have_received(:commit_transaction) - adapter_context.should have_received(:rollback_transaction).once + end.to raise_error 'having a tough time' + expect(adapter_context).to have_received(:begin_transaction).once + expect(adapter_context).not_to have_received(:commit_transaction) + expect(adapter_context).to have_received(:rollback_transaction).once end end end end context "when the adapter doesn't support transactions" do before do - adapter_context.stub(:supports_transactions?) { false } + allow(adapter_context).to receive(:supports_transactions?) { false } end it "run the block on it's own" do - expect { |blk| + expect do |blk| subject.with_message_transaction(&blk) - }.to yield_control - adapter_context.should_not have_received(:begin_transaction) - adapter_context.should_not have_received(:commit_transaction) - adapter_context.should_not have_received(:rollback_transaction) + end.to yield_control + expect(adapter_context).not_to have_received(:begin_transaction) + expect(adapter_context).not_to have_received(:commit_transaction) + expect(adapter_context).not_to have_received(:rollback_transaction) end it 'logs a warning' do allow(logger).to receive(:debug) - expect { |blk| + expect do |blk| subject.with_message_transaction(&blk) - }.to yield_control + end.to yield_control expect(logger).to have_received(:debug).with('this adapter does not support transactions') end end end describe '#ack_message' do let(:message) { double('message') } - let(:options) { {foo: :bar} } + let(:options) { { foo: :bar } } before do allow(message).to receive(:ack) end it 'calls #ack on the message' do subject.ack_message(message) @@ -262,11 +262,11 @@ end end describe '#nack_message' do let(:message) { double('message') } - let(:options) { {foo: :bar} } + let(:options) { { foo: :bar } } before do allow(message).to receive(:nack) end it 'calls #nack on the message' do subject.nack_message(message) @@ -278,96 +278,96 @@ end end describe '#subscribe' do let(:destination) { broker.destination(:my_queue, 'my_queue', exclusive: true) } - let(:consumer_double) { lambda do |_| end } + let(:consumer_double) { ->(_) {} } before do - adapter_context.stub(:subscribe) + allow(adapter_context).to receive(:subscribe) broker.consumer(:my_consumer, &consumer_double) end it 'delegates to the adapter_context' do - adapter_context.should_receive(:subscribe).with(destination, {}) do |&blk| + expect(adapter_context).to receive(:subscribe).with(destination, {}) do |&blk| expect(blk).to be(consumer_double) end subject.subscribe(destination, :my_consumer) end it 'passes the options through' do - options = {foo: :bar} - adapter_context.should_receive(:subscribe).with(destination, options) do |&blk| + options = { foo: :bar } + expect(adapter_context).to receive(:subscribe).with(destination, options) do |&blk| expect(blk).to be(consumer_double) end subject.subscribe(destination, :my_consumer, options) end it 'looks up the destination' do - adapter_context.should_receive(:subscribe).with(destination, {}) do |&blk| + expect(adapter_context).to receive(:subscribe).with(destination, {}) do |&blk| expect(blk).to be(consumer_double) end subject.subscribe(:my_queue, :my_consumer) end context "when the destination can't be found" do let(:bad_dest_name) { :not_a_queue } it 'raises a MessageDriver:NoSuchDestinationError' do - expect { + expect do subject.subscribe(bad_dest_name, :my_consumer) - }.to raise_error(MessageDriver::NoSuchDestinationError, /#{bad_dest_name}/) - adapter_context.should_not have_received(:subscribe) + end.to raise_error(MessageDriver::NoSuchDestinationError, /#{bad_dest_name}/) + expect(adapter_context).not_to have_received(:subscribe) end end context "when the consumer can't be found" do let(:bad_consumer_name) { :not_a_consumer } it 'raises a MessageDriver:NoSuchConsumerError' do - expect { + expect do subject.subscribe(destination, bad_consumer_name) - }.to raise_error(MessageDriver::NoSuchConsumerError, /#{bad_consumer_name}/) - adapter_context.should_not have_received(:subscribe) + end.to raise_error(MessageDriver::NoSuchConsumerError, /#{bad_consumer_name}/) + expect(adapter_context).not_to have_received(:subscribe) end end end describe '#subscribe_with' do let(:destination) { broker.destination(:my_queue, 'my_queue', exclusive: true) } - let(:consumer_double) { lambda do |_| end } + let(:consumer_double) { ->(_) {} } before do - adapter_context.stub(:subscribe) + allow(adapter_context).to receive(:subscribe) end it 'delegates to the adapter_context' do - adapter_context.should_receive(:subscribe).with(destination, {}) do |&blk| + expect(adapter_context).to receive(:subscribe).with(destination, {}) do |&blk| expect(blk).to be(consumer_double) end subject.subscribe_with(destination, &consumer_double) end it 'passes the options through' do - options = {foo: :bar} - adapter_context.should_receive(:subscribe).with(destination, options) do |&blk| + options = { foo: :bar } + expect(adapter_context).to receive(:subscribe).with(destination, options) do |&blk| expect(blk).to be(consumer_double) end subject.subscribe_with(destination, options, &consumer_double) end it 'looks up the destination' do - adapter_context.should_receive(:subscribe).with(destination, {}) do |&blk| + expect(adapter_context).to receive(:subscribe).with(destination, {}) do |&blk| expect(blk).to be(consumer_double) end subject.subscribe_with(:my_queue, &consumer_double) end context "when the destination can't be found" do let(:bad_dest_name) { :not_a_queue } it 'raises a MessageDriver:NoSuchDestinationError' do - expect { + expect do subject.subscribe_with(bad_dest_name, &consumer_double) - }.to raise_error(MessageDriver::NoSuchDestinationError, /#{bad_dest_name}/) - adapter_context.should_not have_received(:subscribe) + end.to raise_error(MessageDriver::NoSuchDestinationError, /#{bad_dest_name}/) + expect(adapter_context).not_to have_received(:subscribe) end end end end end