ArgumentError:SMTP To address可能不为空:[]

时间:2018-05-26 15:41:22

标签: ruby-on-rails ruby rspec rspec-rails

有点困惑,我在我的rails应用程序上运行rspec测试,并有一个型号规格:

it { should validate_uniqueness_of(:email).case_insensitive }

这是电子邮件对象的3个测试中的1个,并且它一直失败并出现以下错误:

1) User should validate that :email is case-insensitively unique
 Failure/Error: it { should validate_uniqueness_of(:email).case_insensitive }

 ArgumentError:
   SMTP To address may not be blank: []
 # ./spec/models/user_spec.rb:43:in `block (2 levels) in <top (required)>'

我无法理解为什么在模型中测试该电子邮件地址需要任何SMTP TO地址。

不确定看什么帮助会很棒,因为它是我唯一的失败测试:(

我认为只有其他可能有用的东西是我跑步:

  • ruby​​ v2.5.0p0
  • rails v5.1.6
  • RSpec 3.7
    • rspec-core 3.7.1
    • rspec-expectations 3.7.0
    • rspec-mocks 3.7.0
    • rspec-rails 3.7.2
    • rspec-support 3.7.1

1 个答案:

答案 0 :(得分:1)

似乎对貌似不相关的元素抛出错误。但是问题实际上出在您添加的用于发送电子邮件的代码中。您需要回顾一下您实际发送电子邮件的代码,然后检查“收件人:”属性。

就我而言,我抱怨工厂毫无意义……至少起初没有。

An error occurred in a `before(:suite)` hook.
Failure/Error: FactoryBot.lint

FactoryBot::InvalidFactoryError:
  The following factories are invalid:

  * attachment - SMTP To address may not be blank: [] (ArgumentError)
  * distance - SMTP To address may not be blank: [] (ArgumentError)
  * structure - SMTP To address may not be blank: [] (ArgumentError)

然后我更深入地挖掘。我将错误跟踪到邮件代码中,或者我以为:

admins = @organization.users.where(role: "org_admin").map(&:email)
mail(to: admins, subject: "New Structure Created")

但是随后其他测试仍然失败。

事实证明,由于to:和电子邮件不存在的某些测试(因为它们确实存在),有时admins字段传递的实际值是 missing 不需要进行那些测试)。因此,我通过简单的if检查“保护”了对邮件发送者的呼叫:

mail(to: admins, subject: "New Structure Created") if admins.any?