简体   繁体   中英

How can I check the code in the exception block?

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM