5
votes

At a bit of a confused here, I am running rspec test on my rails app and have a model spec:

it { should validate_uniqueness_of(:email).case_insensitive }

which is 1 of3 tests for the email object and it keeps failing with the following error:

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)>'

I cant understand why testing that email address in a model would require any SMTP TO address.

Not sure what to look at any help would be great as its my only failing test :(

Only other stuff I would think might help is I running:

  • 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
Can you check you are not setting up SMTP config with blank values somewhere?andHapp
I was getting the password from an ENV var, but to test to make sure that wasnt it i replace it with the password, apart from that no other smtp config anywherePhil
I need to see all the validations on the user model to help you and i need the stacktrace from this errorAnthony
Can you show your mailer code? I'm guessing you're really passing empty array for to: optionkangkyu
Can hard-code the email address - still get this error. Guessing the error is wrong, and something else is the actual problem.JosephK

1 Answers

4
votes

It seems to throw errors about seemingly unrelated elements. Yet the problem is actually in the code you added to send emails. You need to look back through your code where you are actually sending an email and check your "To:" property.

In my case, I got complaints about factories that made no sense... At least not at first.

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)

Then I dug deeper. I tracked down the error to the mailer code, or so I thought:

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

But then other tests still failed.

As it turns out, sometimes the actual value being passed for the to: field was missing due to some tests where admins and emails were not present (because they did not need to be for those tests). So I "protected" the call to the mailer with a simple if check:

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