5

I'm trying to retroactively apply testing to a pretty complicated app (never again). I've built in Sendgrid functionality that sends new users emails when they sign up. As a consequence, when I try to test the signup page, it tries to send my example@example.net email a message, and I get the following error:

Net::SMTPFatalError:
550 Cannot receive from specified address <my app's email>: Unauthenticated senders not allowed

I'm unclear, though, whether this is a problem with how I'm doing testing (something I'm leaving out -- I'm newish), or a deeper problem. Since I switched to Sendgrid from GMail, I've run into errors with the emails that my app should send when I use it on server, though not online. I chalked that up to Sendgrid being a specifically web-app-based email service, but now I'm guessing I'm doing something wrong in my config files. The way I set up my configs is this:

initializers/mail.rb

# SendGrid Settings
ActionMailer::Base.smtp_settings = {
  :address        => 'smtp.sendgrid.net',
  :port           => '587',
  :authentication => :plain,
  :user_name      => ENV['SENDGRID_USERNAME'],
  :password       => ENV['SENDGRID_PASSWORD'],
  :domain         => 'heroku.com'
}
ActionMailer::Base.delivery_method = :smtp

Nothing in my environment.rb file. This question suggests that maybe I should switch those settings into the environment.rb file, but (and clearly, I don't understand the config files too well), I'm wondering if I should have separate settings for development.rb, test.rb, etc. (Also, I tried that switch, and it didn't change the error). And also if there's a workaround for capybara/rspec to get the email request ignored during testing (or somehow deal with its being a fake email address and test the emailing without trying to send one).

Thanks!

Community
  • 1
  • 1
Sasha
  • 6,224
  • 10
  • 55
  • 102

1 Answers1

6

You should be able to set ActionMailer to test mode, either via config:

config.action_mailer.delivery_method = :test

or in your test:

ActionMailer::Base.delivery_method = :test

You can also find more info on testing mailers

bwest
  • 9,182
  • 3
  • 28
  • 58
  • Thanks! When you say "in your test" do you mean in the test.rb file? Or in my user_pages_spec.rb file (where I'm actually using capybara)? – Sasha Nov 20 '12 at 20:41
  • Yes, directly from your capybara spec, before you attempt to send the email. – bwest Nov 20 '12 at 20:53
  • Awesome! Worked like a charm. – Sasha Nov 20 '12 at 21:05
  • Great! If you have the time it's probably better to do it in your config files e.g. test.rb and development.rb, so you can do things like have development actually send a message but capture it with something like [mailcatcher](http://mailcatcher.me/) – bwest Nov 21 '12 at 16:41
  • 1
    Definitely put it in your config file. It's much safer and will work in rspec too. Also, you should have a look at https://github.com/bmabey/email-spec for testing email stuff. – Swift Nov 21 '12 at 19:33