mail.perform_deliveries = false在ActionMailer拦截器中不起作用

Bri*_*ong 3 ruby-on-rails actionmailer

我们正在使用这样的ActionMailer拦截器:

class MailerInterceptor
  def self.delivering_email(message)
    p 1
    if message.to.include?("test2@example.com")
      p 2
      message.perform_deliveries = false
    end
  end
end

Mailer.register_interceptor(MailerInterceptor)
Run Code Online (Sandbox Code Playgroud)

但它似乎没有阻止在生产或我们的测试中向该地址发送消息:

require 'spec_helper'

describe "MailerInterceptor" do
  it "should block sending to certain addresses" do
    expect{ Mailer.user_alert("test1", "test@example.com").deliver }.to change{ ActionMailer::Base.deliveries.count }.by(1)
    expect{ Mailer.user_alert("test2", "test2@example.com").deliver }.to_not change{ ActionMailer::Base.deliveries.count }
  end
end
Run Code Online (Sandbox Code Playgroud)

这是使用rails 3.2.14.

它在测试的第二行打印"2",但仍然发送电子邮件.有任何想法吗?谢谢!

小智 7

请检查您是否使用ActionMailer #delivery方法,而不是#delivery!因为第二个跳过检查perform_deliveries.