如何在测试环境中关闭 ActionMailer?
How to turn off ActionMailer in test environment?
当我 运行 使用 RSpec 进行测试时,我收到了真实的电子邮件。
我已经完成了 this question 中所写的所有内容(我添加了更改我的环境配置以不发送电子邮件,但它根本没有任何效果。我认为我的设置在某处被替换了但是我找不到位置。
这是我的测试环境配置 (config/enviroments/test.rb)
Myapp::Application.configure do
config.action_mailer.delivery_method = :test
config.action_mailer.perform_deliveries = false
end
这是我的邮件:
class ApplicationMailer < ActionMailer::Base
default from: 'hello@myapp.com'
layout 'mailer'
ADMIN_EMAIL = 'admin@myapp.com'
end
class AdminMailer < ApplicationMailer
default to: "#{ADMIN_EMAIL}"
def mandrill_client
@mandrill_client ||= Mandrill::API.new MANDRILL_API_KEY
end
def new_user(user)
#set required params
mandrill_client.messages.send_template template_name, template_content, message
end
end
看来我漏掉了一些非常明显的东西。但是我需要你的帮助来分解它。
由于您使用 Mandrill::API
发送电子邮件,而不是 ActionMailer
,您对 ActionMailer
的设置对它完全没有影响。
您应该在 Mandrill::API
中找到相应的设置,或者存根用于测试的发送方法。
实际上我建议您仍然使用 ActionMailer
和 Mandrill SMTP 服务器:
config.action_mailer.smtp_settings = {
address: 'smtp.mandrillapp.com',
port: 587,
domain: Rails.application.config_for(:global)['domain'],
authentication: 'plain',
enable_starttls_auto: true,
user_name: Rails.application.secrets.email_provider_username,
password: Rails.application.secrets.email_provider_apikey
}
当我 运行 使用 RSpec 进行测试时,我收到了真实的电子邮件。 我已经完成了 this question 中所写的所有内容(我添加了更改我的环境配置以不发送电子邮件,但它根本没有任何效果。我认为我的设置在某处被替换了但是我找不到位置。
这是我的测试环境配置 (config/enviroments/test.rb)
Myapp::Application.configure do
config.action_mailer.delivery_method = :test
config.action_mailer.perform_deliveries = false
end
这是我的邮件:
class ApplicationMailer < ActionMailer::Base
default from: 'hello@myapp.com'
layout 'mailer'
ADMIN_EMAIL = 'admin@myapp.com'
end
class AdminMailer < ApplicationMailer
default to: "#{ADMIN_EMAIL}"
def mandrill_client
@mandrill_client ||= Mandrill::API.new MANDRILL_API_KEY
end
def new_user(user)
#set required params
mandrill_client.messages.send_template template_name, template_content, message
end
end
看来我漏掉了一些非常明显的东西。但是我需要你的帮助来分解它。
由于您使用 Mandrill::API
发送电子邮件,而不是 ActionMailer
,您对 ActionMailer
的设置对它完全没有影响。
您应该在 Mandrill::API
中找到相应的设置,或者存根用于测试的发送方法。
实际上我建议您仍然使用 ActionMailer
和 Mandrill SMTP 服务器:
config.action_mailer.smtp_settings = {
address: 'smtp.mandrillapp.com',
port: 587,
domain: Rails.application.config_for(:global)['domain'],
authentication: 'plain',
enable_starttls_auto: true,
user_name: Rails.application.secrets.email_provider_username,
password: Rails.application.secrets.email_provider_apikey
}