I가 2 개 delivery_method
의, 그래서 같은 환경 변수를 사용하여 전환 :RSpec에서 ActionMailer delivery_method를 테스트하는 방법?
config.action_mailer.delivery_method = :mailjet
또는
config.action_mailer.delivery_method = :smtp
방법 RSpec에 시험에
delivery_method
을 테스트합니까?
I가 2 개 delivery_method
의, 그래서 같은 환경 변수를 사용하여 전환 :RSpec에서 ActionMailer delivery_method를 테스트하는 방법?
config.action_mailer.delivery_method = :mailjet
또는
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