加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

Rails 4,RSpec 3.2 – 如何模拟ActionMailer的deliver_now方法来

发布时间:2020-12-17 01:21:36 所属栏目:百科 来源:网络整理
导读:环境 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 我有以下方法 def send_event_alert_email(event_id) event = Event.find_by(id: event_id) ... ... ... EventAlertMaile
环境

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

我有以下方法

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

我需要编写规范,以确保在EventAlertMailer.event_alert(event_id).deliver_now引发任何异常时不会调用create_alert(event).所以基本的问题是我如何模拟deliver_now来引发它可能实际抛出的可能异常.

解决方法

以下是测试deliver_now方法的方法:
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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读