【问题标题】:How to test ActionMailer delivery_method in RSpec?如何在 RSpec 中测试 ActionMailer delivery_method?
【发布时间】:2017-03-10 05:22:21
【问题描述】:

我有 2 个delivery_methods,可以使用如下环境变量进行切换:

config.action_mailer.delivery_method = :mailjet

config.action_mailer.delivery_method = :smtp

如何在 RSpec 测试中测试delivery_method

【问题讨论】:

    标签: ruby-on-rails ruby ruby-on-rails-4 rspec rspec-rails


    【解决方案1】:

    你的意思是,测试设置是否设置?

    require "rails_helper"
    
    RSpec.describe "Rails application configuration" do
      it "set the delivery_method to test" do
        expect(Rails.application.config.action_mailer.delivery_method).to eql :test
        expect(ActionMailer::Base.delivery_method).to eql :test
      end
    end
    

    或者它正在使用类似于 SMTP 库之类的东西?

    require "rails_helper"
    require "net/smtp"
    
    RSpec.describe "Mail is sent via Net::SMTP" do
      class MockSMTP
        def self.deliveries
          @@deliveries
        end
    
        def initialize
          @@deliveries = []
        end
    
        def sendmail(mail, from, to)
          @@deliveries << { mail: mail, from: from, to: to }
          'OK'
        end
    
        def start(*args)
          if block_given?
            return yield(self)
          else
            return self
          end
        end
      end
    
      class Net::SMTP
        def self.new(*args)
          MockSMTP.new
        end
      end
    
      class ExampleMailer < ActionMailer::Base
        def hello
          mail(to: "smtp_to", from: "smtp_from", body: "test")
        end
      end
    
      it "delivers mail via smtp" do
        ExampleMailer.delivery_method = :smtp
        mail = ExampleMailer.hello.deliver_now
    
        expect(MockSMTP.deliveries.first[:mail]).to eq mail.encoded
        expect(MockSMTP.deliveries.first[:from]).to eq 'smtp_from'
        expect(MockSMTP.deliveries.first[:to]).to eq %w(smtp_to)
      end
    end
    

    Max's answer 一样,您真的不能真正成为邮件程序delivery_method,因为它是一种服务,可能存在也可能不存在于您的测试环境中,因此经常被排除在外;例如

     # config/environments/test.rb
     # Tell Action Mailer not to deliver emails to the real world.
     # The :test delivery method accumulates sent emails in the
     # ActionMailer::Base.deliveries array.
     config.action_mailer.delivery_method = :test
    

    【讨论】:

      【解决方案2】:

      你没有。你没有真正测试 BDD 或 TDD 中的配置细节,因为那是每个环境的,通常只是一个坏主意。

      测试环境中的邮件程序设置为仅将电子邮件添加到假脱机,以便您可以预期/断言电子邮件已发送。

        # config/environments/test.rb
        # Tell Action Mailer not to deliver emails to the real world.
        # The :test delivery method accumulates sent emails in the
        # ActionMailer::Base.deliveries array.
        config.action_mailer.delivery_method = :test
      

      通常做的是在生产环境中手动测试电子邮件配置。您可以从控制台执行此操作,或者如果您经常这样做,请创建一个 rake task

      【讨论】:

        猜你喜欢
        • 2011-08-16
        • 2012-06-09
        • 2013-11-27
        • 2011-05-07
        • 2012-04-10
        • 2014-09-15
        • 2011-12-19
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多