Dan Rubio
Dan Rubio

Reputation: 4897

How can I force an ActionMailer error for testing?

I have this code that sends an email based on conditions. If there is an error its supposed to catch the account that wasn't able to be sent in an array like so:

def process_email(delivery_method_name)
  begin
    Account::Access.new(account).spam! if delivery_method_name == 'mark_as_spam'
    AccountMailer.send("#{delivery_method_name}", account).deliver_now
  rescue
    @first_notification_error << account.id
    @second_notification_failure << account.id
    @third_notification_failure << account.id
    @fourth_notification_error << account.id
    @fourth_notification_failure << account.id
    return
  end
  update_reverification_fields
end

So in my test.rb file I want to be able to test that the account.id was caught inside of the @first_notification_error and other containers. It's not exactly clear to me though how to do this though. I read in another post to place this code in development.rb and/or test.rb config.action_mailer.raise_delivery_errors = true but I don't think this is what I'm looking for. Is there a way I can raise the error in my test, perhaps with a stub or something similar?

Upvotes: 1

Views: 248

Answers (1)

Aleksey Shein
Aleksey Shein

Reputation: 7482

You need to mock AccountMailer. Put this line before calling the code you're testing:

delivery_method = 'some method on mailer that you want to mock'
allow(AccountMailer).to receive(delivery_method).and_raise("boom")

Upvotes: 1

Related Questions