2017-03-10 15 views

答えて

0

あなたはしていません。 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を作成してください。

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ライブラリのようなものを使用していること、すべての下に使用されていますか? Max's answer同様

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 

それはしてもしなくてもよいテスト環境で存在することができるサービスであるため、多くの場合、スタブアウトされているので、あなたが本当に本当にメーラー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 
関連する問題