【问题标题】:How to write an RSpec for a Mail Interceptor?如何为邮件拦截器编写 RSpec?
【发布时间】:2011-10-03 19:00:44
【问题描述】:

我正在使用如下邮件拦截器:

setup_mail.rb

Mail.register_interceptor(MailInterceptor) if Rails.env != "production" 

邮件拦截器类

class MailInterceptor  
  def self.delivering_email(message)  
    message.subject = "#{message.subject} [#{message.to}]"
    message.to = "xxxxx@xxxxxx.com"
  end
end

我无法为此拦截器创建 rspec,因为 rake 规范不会发生这种情况。

我有以下规格:

  describe "MailInterceptor" do 
    it "should be intercepted" do
      @email = UserMailer.registration_automatically_generated(@user)
      @email.should deliver_to("xxxxx@xxxxxx.com")      
    end
  end

在 test.log 中,我看到 Deliver_to 不是拦截器。关于如何为拦截器编写 rspec 的任何想法?

谢谢

【问题讨论】:

  • 拦截器在开发中工作吗? setup_mail.rb 存储在哪里?
  • 是的,它是在 initalizers/setup_mail.rb w Mail.register_interceptor(MailInterceptor) if Rails.env != "production" 中设置的

标签: ruby-on-rails ruby-on-rails-3 rspec email-spec


【解决方案1】:

deliver_to 的 email_spec 匹配器实际上并没有通过典型的传递方法运行邮件消息,它是 simply inspects the message 将其发送给谁。

要测试你的拦截器,你可以直接调用 delivery_email 方法

it 'should change email address wen interceptor is run' do
  email = UserMailer.registration_automatically_generated(@user)
  MailInterceptor.delivering_email(email)
  email.should deliver_to('xxxxx@xxxxxx.com')
end

另一种选择是让邮件正常发送,并使用 email_spec 的 last_email_sent 测试它是否发送到正确的地方

it 'should intercept delivery' do
  reset_mailer
  UserMailer.registration_automatically_generated(@user).deliver
  last_email_sent.should deliver_to('xxxxx@xxxxxx.com')
end

使用这两个测试可能是个好主意,首先要确保MailInterceptor 正在按照您的预期改变消息。第二个测试更像是一个集成测试,测试MailInterceptor 是否与交付系统挂钩。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-01-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-08-19
    • 2012-11-20
    • 1970-01-01
    相关资源
    最近更新 更多