spec/units/message_driver/message_spec.rb in message-driver-0.2.2 vs spec/units/message_driver/message_spec.rb in message-driver-0.3.0

- old
+ new

@@ -4,52 +4,89 @@ describe Base do describe "#initialize" do let(:body) { "The message body" } let(:headers) { { foo: :bar, bar: :baz} } let(:properties) { {persistent: true, client_ack: true} } + let(:ctx) { double("adapter_context") } context "sets the body, header and properites on initialization" do - subject { described_class.new(body, headers, properties) } + subject { described_class.new(ctx, body, headers, properties) } + its(:ctx) { should be(ctx) } its(:body) { should eq(body) } its(:headers) { should eq(headers) } its(:properties) { should eq(properties) } end end - subject(:message) { described_class.new("body", {}, {}) } + let(:logger) { MessageDriver.logger } + let(:ctx) { double("adapter_context") } + let(:options) { double("options") } + subject(:message) { described_class.new(ctx, "body", {}, {}) } describe "#ack" do - let(:options) { {foo: :bar} } - before do - MessageDriver::Client.stub(:ack_message) + allow(ctx).to receive(:ack_message) end - it "passes itself to Client.ack_message" do - subject.ack - expect(MessageDriver::Client).to have_received(:ack_message).with(subject, {}) + context "when the adapter supports client acks" do + before do + allow(ctx).to receive(:supports_client_acks?) { true } + end + it "calls #ack_message with the message" do + subject.ack + expect(ctx).to have_received(:ack_message).with(subject, {}) + end + it "passes the supplied options to ack_message" do + subject.ack(options) + expect(ctx).to have_received(:ack_message).with(subject, options) + end end - - it "passes the options to Client.ack_message" do - subject.ack(options) - expect(MessageDriver::Client).to have_received(:ack_message).with(subject, options) + context "when the adapter doesn't support client acks" do + before do + allow(ctx).to receive(:supports_client_acks?) { false } + end + it "doesn't call #ack_message" do + subject.ack + expect(ctx).not_to have_received(:ack_message) + end + it "logs a warning" do + allow(logger).to receive(:debug) + subject.ack + expect(logger).to have_received(:debug).with("this adapter does not support client acks") + end end end describe "#nack" do - let(:options) { {foo: :bar} } - before do - MessageDriver::Client.stub(:nack_message) + allow(ctx).to receive(:nack_message) end - it "passes itself to Client.nack_message" do - subject.nack - expect(MessageDriver::Client).to have_received(:nack_message).with(subject, {}) + context "when the adapter supports client nacks" do + before do + allow(ctx).to receive(:supports_client_acks?) { true } + end + it "calls #nack_message with the message" do + subject.nack + expect(ctx).to have_received(:nack_message).with(subject, {}) + end + it "passes the supplied options to nack_message" do + subject.nack(options) + expect(ctx).to have_received(:nack_message).with(subject, options) + end end - - it "passes the options to Client.nack_message" do - subject.nack(options) - expect(MessageDriver::Client).to have_received(:nack_message).with(subject, options) + context "when the adapter doesn't support client nacks" do + before do + allow(ctx).to receive(:supports_client_acks?) { false } + end + it "doesn't call #nack_message" do + subject.nack + expect(ctx).not_to have_received(:nack_message) + end + it "logs a warning" do + allow(logger).to receive(:debug) + subject.nack + expect(logger).to have_received(:debug).with("this adapter does not support client acks") + end end end end end