How can I check the code in the exception block?

634 views Asked by At

I have Sidekiq worker.

class DeliverSmsMessageWorker
  include Sidekiq::Worker

  def perform(sms_message_id)
    ....
  rescue StandardError => e
    Rails.logger.error("SmsMessageWorker ERROR: #{e}")
    Bugsnag.notify(e)
  end
end

And i write spec, but i get error when i try test Rails.looger.

describe DeliverSmsMessageWorker, type: :worker do
  subject(:worker) { DeliverSmsMessageWorker }

  context 'on exceptions' do
    let(:error) { StandardError.new('test exception') }

    before do
      allow(worker).to receive(:perform_async).with(sms_message.id).and_raise(error)
    end

    it 'message in logger' do

      Sidekiq::Testing.inline! do
        worker.perform_async(sms_message.id)
        expect(Rails.logger).to receive(:error).and_call_original
      end
    end
  end
end

After when i run this specs, I get the error. but why?

Is there any point in testing these two lines?

1) DeliverSmsMessageWorker on exceptions message in the logger
 Failure/Error: worker.perform_async(sms_message.id)

 StandardError:
   test exception
1

There are 1 answers

0
Vasilisa On

Maybe you should use the block syntax for raise_error here:

  Sidekiq::Testing.inline! do
    expect { worker.perform_async(sms_message.id) }.to raise_error { |error|
      expect(Rails.logger).to receive(:error).and_call_original
    }
  end