Ruby on rails 如何在RSpec中测试ActionMailer交付方法?
我有2个Ruby on rails 如何在RSpec中测试ActionMailer交付方法?,ruby-on-rails,ruby,ruby-on-rails-4,rspec,rspec-rails,Ruby On Rails,Ruby,Ruby On Rails 4,Rspec,Rspec Rails,我有2个delivery\u方法s,可使用如下环境变量进行切换: config.action_mailer.delivery_method = :mailjet 或 如何在RSpec测试中测试delivery\u方法?您不需要。您不需要在BDD或TDD中真正测试配置细节,因为这是基于环境的,通常是个坏主意 相反,测试环境中的邮件程序设置为只向假脱机添加电子邮件,以便您可以预期/断言电子邮件已发送 # config/environments/test.rb # Tell Action M
delivery\u方法
s,可使用如下环境变量进行切换:
config.action_mailer.delivery_method = :mailjet
或
如何在RSpec测试中测试
delivery\u方法?您不需要。您不需要在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
通常的做法是在生产环境中手动测试电子邮件配置。您可以从控制台执行此操作,或者如果经常执行此操作,则可以创建一个。您的意思是,测试是否设置了此设置
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
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
# 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