私はそうのように環境変数を使用して切り替え、2 delivery_method
秒を持っている:RSpecでActionMailer delivery_methodをテストするには?
config.action_mailer.delivery_method = :mailjet
OR
config.action_mailer.delivery_method = :smtp
どのようにRSpecのテストでdelivery_method
をテストするのですか?
私はそうのように環境変数を使用して切り替え、2 delivery_method
秒を持っている:RSpecでActionMailer delivery_methodをテストするには?
config.action_mailer.delivery_method = :mailjet
OR
config.action_mailer.delivery_method = :smtp
どのようにRSpecのテストでdelivery_method
をテストするのですか?
あなたはしていません。 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を作成してください。
設定が設定されているかどうかをテストしていますか?
は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