require "spec_helper" require "click_session/configuration" require "support/test_unit_model" describe ClickSession::FailureStatusReporter do describe "#report" do let(:model) do create(:test_unit_model) end before do stub_notifier_in_configuration end it "rejects shippings not processed" do session_state = build_session_state_in_not_processed_state status_reporter = ClickSession::FailureStatusReporter.new expect { status_reporter.report(session_state) }. to raise_error(ArgumentError) end it "rejects shippings successfully processed" do session_state = build_session_state_in_successfully_processed_state status_reporter = ClickSession::FailureStatusReporter.new expect { status_reporter.report(session_state) }. to raise_error(ArgumentError) end it "reports with an ERROR status" do stub_notifier_in_configuration session_state = build_session_state_in_failed_state webhook_stub = ClickSession::Webhook.new("failure.url") allow(webhook_stub).to receive(:call) status_reporter = ClickSession::FailureStatusReporter.new( webhook_stub ) status_reporter.report(session_state) expect(webhook_stub). to have_received(:call). with({ id: session_state.id, status: { success: false, message: "See error logs" } }) end def stub_notifier_in_configuration notifier_double = class_double(ClickSession::Notifier) notifier_stub = ClickSession::Notifier.new # Stub the methods allow(notifier_stub). to receive(:session_reported) # Add the stub to the double allow(notifier_double). to receive(:new). and_return(notifier_stub) # Make the configuration return our double allow(ClickSession.configuration). to receive(:notifier_class). and_return(notifier_double) notifier_stub end def build_session_state_in_successfully_processed_state ClickSession::SessionState.create(state: 1, model_record: model.id) end def build_session_state_in_failed_state ClickSession::SessionState.create(state: 2, model_record: model.id) end def build_session_state_in_not_processed_state ClickSession::SessionState.create(state: 0, model_record: model.id) end end end