Rails 4, RSpec 3.2 - how to mockup ActionMailer's deliver_now method to raise exceptions
Asked Answered
B

1

6

Environment

Ruby 2.2.1, Rails 4.2.0, rspec-core 3.2.2, rspec-expectations 3.2.0, rspec-mocks 3.2.1, rspec-rails 3.2.1, rspec-support 3.2.2

I have the following method

def send_event_alert_email(event_id)
  event = Event.find_by(id: event_id)
  ...
  ...     
  ...
  EventAlertMailer.event_alert(event_id).deliver_now

  create_alert(event)
end

I need to write specs which makes sure that create_alert(event) doesn't get invoked when EventAlertMailer.event_alert(event_id).deliver_now raises any exceptions. So the basic question is how do I simulate deliver_now to raise possible exceptions which it might actually throw.

Bibbye answered 1/5, 2015 at 11:42 Comment(0)
C
6

Here is how to test deliver_now method:

describe EventAlertMailer do
  it 'sends email' do
    delivery = double
    expect(delivery).to receive(:deliver_now).with(no_args)

    expect(EventAlertMailer).to receive(:event_alert)
      .with(event_id)
      .and_return(delivery)

    MyClass.send_event_alert_email
  end
end
Christachristabel answered 20/8, 2015 at 19:36 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.