【问题标题】:User.invite! not sending email in tests用户邀请!在测试中不发送电子邮件
【发布时间】:2013-03-25 23:53:04
【问题描述】:
我正在尝试为我的用户模型创建一些测试。当我使用 devise_invitable User.invite 邀请用户加入我的应用程序时!创建一个用户,但似乎没有记录电子邮件。 ActionMailer::Base.deliveries.count == 0。在生产电子邮件中发送,当我在测试中使用我的客户电子邮件时,ActionMailer::Base.deliveries.count 是正确的数字。
在 devise_invitable 中是否缺少一些东西来配置测试的电子邮件发送?
【问题讨论】:
标签:
ruby-on-rails
ruby
devise
devise-invitable
【解决方案1】:
我在两个环境的测试和开发中都遇到了同样的问题,并注意到它在日志中默默地返回了ArgumentError - An SMTP To address is required to send a message. Set the message smtp_envelope_to, to, cc, or bcc address.。
我没有使用Devise::InvitationsController,因此在我的情况下调用confirm! 是必要的,因为:confirmable 已启用。
User.invite!(invite_params, current_user) do |u|
u.confirm!
end
devise_invitable (1.4.0)
【解决方案2】:
我相信这不是 Devise 的东西,Rails 默认情况下实际上不会在测试环境中发送电子邮件,你可能有类似的行
# 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
在您的 config/environments/test.rb 上,为了实际发送电子邮件,您可以评论此行并使用您的 production.rb 的 ActionMailer 配置,但不建议这样做,因为您可以使用 ActionMailer::Base .deliveries 数组本身来测试您的电子邮件。