Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sendgrid Error in Capybara Testing

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 [email protected] 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!

like image 356
Sasha Avatar asked Nov 20 '12 18:11

Sasha


1 Answers

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

like image 143
bwest Avatar answered Sep 22 '22 03:09

bwest