我正在尝试找到编写rspec测试的最佳方法,该方法将指定对邮件(mail_content) .deliver并引发一个异常,这样我就可以断言Rails。
我知道你不是要嘲笑被测试的类,但这适用于超类吗?
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
使用RSpec可以存根引发异常。请看下面的代码片段:
whatever.should_receive(:do_some_stuff).and_raise(ArgumentError)