如何在Ruby中使用rspec测试超类方法引发异常

如何在Ruby中使用rspec测试超类方法引发异常,ruby,methods,rspec,superclass,specifications,Ruby,Methods,Rspec,Superclass,Specifications,我正在试图找到编写rspec测试的最佳方法,该测试将规范对的调用 邮件(邮件内容)。传递 并引发一个异常,这样我就可以断言调用了Rails.logger 我知道你不打算模仿测试中的类,但这是否适用于超级类 class MyMailer < ActionMailer::Base default from: 'test@test.com' default charset: 'UTF-8' default content_type: 'text/plain' default su

我正在试图找到编写rspec测试的最佳方法,该测试将规范对的调用 邮件(邮件内容)。传递 并引发一个异常,这样我就可以断言调用了Rails.logger

我知道你不打算模仿测试中的类,但这是否适用于超级类

class MyMailer < ActionMailer::Base
  default from: 'test@test.com'
  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
classmymailere
Rails.logger.error“未能通过电子邮件发送订单响应-#{e}”
结束
结束
结束
结束

使用RSpec,您可以引发异常。请参阅以下代码段:

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