【问题标题】:Test fails when sending email: An SMTP To address is required to send a message. Set the message smtp_envelope_to, to, cc, or bcc address发送电子邮件时测试失败:发送邮件需要 SMTP 收件人地址。设置消息 smtp_envelope_to、to、cc 或 bcc 地址
【发布时间】:2013-12-02 14:57:40
【问题描述】:

我有一个用户模型的 after_create 回调,它将向管理员发送一封电子邮件,以便他可以批准该帐户。它在开发模式下完美运行,但是当我运行 Capybara/RSpec 测试时,它失败并出现以下异常:

ArgumentError:
   An SMTP To address is required to send a message. Set the message smtp_envelope_to, to, cc, or bcc address.

我确实在 config/environments/test.rb 中将 config.action_mailer.delivery_method 设置为 :test。为什么它在测试环境中尝试使用 SMTP?为什么仅此电子邮件交付(模型中)会发生这种情况,而其他交付(使用 devise gem)不会发生这种情况?

这是来自 User 模型的 sn-p:

class User < ActiveRecord::Base
  after_create :send_admin_mail
protected

  def send_admin_mail
    puts ActionMailer::Base.delivery_method  #prints test!
    ReviewMailer.new_user_waiting_for_approval(self).deliver
  end
end

【问题讨论】:

    标签: ruby-on-rails rspec capybara actionmailer


    【解决方案1】:

    尝试设置:

    config.action_mailer.raise_delivery_errors = false
    

    在您的 test.rb 文件中,如果它不存在,请添加它。

    【讨论】:

    • 我不会将禁用错误消息称为解决问题的好方法。他们很可能仍然没有解决。在我的情况下,这样的错误实际上意味着我没有正确地传递给或从地址传递给邮件。
    【解决方案2】:

    错误提示您缺少to 地址。由于您正在向管理员发送电子邮件,我猜这封电子邮件与 Rails 环境不同,并且您已在某处进行了配置。确保为测试环境配置了一个。

    【讨论】:

    • 感谢@jvnill,我的问题是DatabaseCleaner 在每次测试之前都会截断所有表,因此默认的AdminUser 也会被删除。异常中的 SMTP 这个词让我很困惑,我要换个话题。
    猜你喜欢
    • 2013-08-27
    • 2016-03-29
    • 1970-01-01
    • 2012-04-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-01-21
    • 2017-04-30
    相关资源
    最近更新 更多