spec/support/shared/subscription_examples.rb in message-driver-0.4.0 vs spec/support/shared/subscription_examples.rb in message-driver-0.5.0

- old
+ new

@@ -1,24 +1,24 @@ -shared_examples 'subscriptions are not supported' do +RSpec.shared_examples 'subscriptions are not supported' do describe '#supports_subscriptions?' do it 'returns false' do expect(subject.supports_subscriptions?).to eq(false) end end describe '#subscribe' do it 'raises an error' do destination = double('destination') - consumer = lambda do |_| end - expect { + consumer = ->(_) {} + expect do subject.subscribe(destination, &consumer) - }.to raise_error "#subscribe is not supported by #{subject.adapter.class}" + end.to raise_error "#subscribe is not supported by #{subject.adapter.class}" end end end -shared_examples 'subscriptions are supported' do |subscription_type| +RSpec.shared_examples 'subscriptions are supported' do |subscription_type| describe '#supports_subscriptions?' do it 'returns true' do expect(subject.supports_subscriptions?).to eq(true) end end @@ -34,13 +34,11 @@ end end describe '#subscribe' do before do - if destination.respond_to? :purge - destination.purge - end + destination.purge if destination.respond_to? :purge end let(:subscription) { adapter_context.subscribe(destination, &consumer) } after do subscription.unsubscribe @@ -51,22 +49,31 @@ end context 'the subscription' do subject { subscription } - it { should be_a MessageDriver::Subscription::Base } - it { should be_a subscription_type } - its(:adapter) { should be adapter } - its(:destination) { should be destination } - its(:consumer) { should be consumer } + it { is_expected.to be_a MessageDriver::Subscription::Base } + it { is_expected.to be_a subscription_type } + describe '#adapter' do + it { expect(subject.adapter).to be adapter } + end + + describe '#destination' do + it { expect(subject.destination).to be destination } + end + + describe '#consumer' do + it { expect(subject.consumer).to be consumer } + end + describe '#unsubscribe' do it "makes it so messages don't go to the consumer any more" do subscription.unsubscribe - expect { + expect do destination.publish('should not be consumed') - }.to_not change{messages.size} + end.to_not change { messages.size } end end end context 'when there are already messages in the destination' do @@ -74,62 +81,62 @@ destination.publish(message1) destination.publish(message2) end it 'plays the messages into the consumer' do - expect { + expect do subscription pause_if_needed - }.to change{messages.size}.from(0).to(2) + end.to change { messages.size }.from(0).to(2) bodies = messages.map(&:body) expect(bodies).to include(message1) expect(bodies).to include(message2) end it 'removes the messages from the queue' do pause_if_needed - expect { + expect do subscription pause_if_needed - }.to change{destination.message_count}.from(2).to(0) + end.to change { destination.message_count }.from(2).to(0) end end context 'when a message is published to the destination' do before do subscription end it 'consumers the message into the consumer instead of putting them on the queue' do - expect { - expect { + expect do + expect do subject.publish(destination, message1) pause_if_needed - }.to change{messages.length}.from(0).to(1) - }.to_not change{destination.message_count} + end.to change { messages.length }.from(0).to(1) + end.to_not change { destination.message_count } expect(messages[0].body).to eq(message1) end end context 'when the consumer raises an error' do let(:error) { RuntimeError.new('oh nos!') } let(:consumer) do lambda do |_| - raise error + fail error end end before do destination.publish(message1) destination.publish(message2) end it 'keeps processing the messages' do pause_if_needed - expect { + expect do subscription pause_if_needed - }.to change{destination.message_count}.from(2).to(0) + end.to change { destination.message_count }.from(2).to(0) end context 'an error_handler is provided' do let(:error_handler) { double(:error_handler, call: nil) } let(:subscription) { adapter_context.subscribe(destination, error_handler: error_handler, &consumer) }