2
votes

I am trying to find the best way of writing an rspec test that will spec the call to mail(mail_content).deliver and raise an exception so I can assert the Rails.logger is called.

I know you are not meant to mock the class under test but does this apply to super classes?

class MyMailer < ActionMailer::Base
  default from: '[email protected]'
  default charset: 'UTF-8'
  default content_type: 'text/plain'
  default subject: 'Test'

  def send_email (to, mail_headers_hash, mail_body)
    begin
      headers mail_headers_hash
      mail_content = {to: to, body: mail_body}
      mail(mail_content).deliver

     rescue ArgumentError, StandardError => e
    Rails.logger.error "Failed to email order response - #{e}"
  end
end
end
end
1

1 Answers

1
votes

With RSpec you could stub raising exception. See the following code snippet:

whatever.should_receive(:do_some_stuff).and_raise(ArgumentError)