How do you disable Action Mailer in Rails 4 when running tests? - ruby-on-rails

I'm trying to disable Action Mailer from actually sending out emails in Rails 4 and I am wondering how I might go about doing this. In my config/environments/test.rb file I have these two lines:
config.action_mailer.delivery_method = :test
config.action_mailer.perform_deliveries = false
and I also tried to place this line in the environment.rb file:
ActionMailer::Base.delivery_method = :test
as was suggested by other posts but these methods don't seem to work.
Can anyone provide me with any suggestions or advice on how to disable Action Mailer when I am running tests?
Thanks

Related

Best practice for not delivering certain mails in development

We have a center/course platform. Every time a new student is interested in a course we send an email to de center to notify them about it.
However, we don't want this to happen in development, as centers would receive 'false' notifications.
Firs thing comes to my mind is introducing unless Rails.env == 'production' wherever we don't want to send an email. But it feels a little bit weird.
What would it be the best solution/pattern to accomplish this?
Note that we don't want to stop sending mails to everyone, but just some of them.
Thanks in advance.
In your config/environments/development.rb (and test.rb) you can do:
Rails.application.configure do
#.......other config here
config.action_mailer.delivery_method = :test
end
This does not send the email, but you can see it in the log file/terminal output
I think what you're looking for is this:
Add this to your config/environments/development.rb
config.action_mailer.perform_deliveries = false
or
config.action_mailer.delivery_method = :test
to log the deliveries
(If you're using ActionMailer)

RSpec feature test for inherited Devise Registrations Controller - "Errno::EADDRNOTAVAIL: Cant assign requested address"

I have a Rails 4.2 app using Devise. I had to customize the Registrations controller in #create, and whenever I run my feature specs, I keep getting the error:
Errno::EADDRNOTAVAIL: Cant assign requested address - connect(2) for nil port 0
I'm out of ideas. What's going on? All my devise controller specs work fine, testing manually works fine, but what configuration remains?
My feature spec:
/spec/features/visitor/visitor_can_signup_spec.rb
Turns out i had my config/environments/test.rb had the wrong mailer setting.
I had
config.action_mailer.delivery_method = :smtp
so i changed it to:
config.action_mailer.delivery_method = :test

How to use a route helper method from a file in the lib directory?

I need to use the root_url method from a method defined in a file in the lib folder. Is that possible?
I tried including this line in my class:
include Rails.application.routes.url_helpers
but this gives me the error
Missing host to link to! Please provide :host parameter or set default_url_options[:host]
Edit: I found out that it works if I first initialize the routes:
def initialize_routes
if Rails.env.development? || Rails.env.test?
Rails.application.routes.default_url_options[:host] = 'localhost:3000'
elsif Rails.env.production?
Rails.application.routes.default_url_options[:host] = 'example.com'
end
end
Is there a better way to accomplish this? Maybe setting the routes in a config file?
#JDutil's #3 solution, as mentioned in the comments, is configuring the action mailer and not the router's routes. However, in the configuration you can perform the following:
In config/environments/development.rb and config/environments/test.rb:
MyApp::Application.configure do
# other configuration ...
config.after_initialize do
Rails.application.routes.default_url_options[:host] = 'localhost:3000'
end
end
There should be a couple solutions for this.
1) when using root_url pass in the host param like:
root_url(:host => 'localhost')
That would need to be made to be environment specific though.
2) You should also be able to set the routes default_url_options like:
Rails.application.routes.default_url_options[:host]= 'localhost:3000'
3) Within your environment config files you should set the default_url_options as stated in the error. For example:
In config/environments/development.rb:
config.action_mailer.default_url_options[:host] = 'localhost'
In config/environments/test.rb:
config.action_mailer.default_url_options[:host] = 'localhost'
In config/environments/production.rb:
config.action_mailer.default_url_options[:host] = 'production.com'
If you're relying on url_helpers for something like ActiveModel::Serializers (at least the v0.9.* series) where you'll be generating URLs outside of the main Rails request/response flow, you'll also need to set the Rails.application.routes.default_url_options. I've done it like so,
# config/application.rb
config.after_initialize do
Rails.application.routes.default_url_options = config.action_mailer.default_url_options
end
The url helpers are now available, with a default host, in places like background jobs, etc...
I had this exact same issue and none of the configuration suggestions worked. Thing that left me confused is that url_helpers works just fine from other parts of the application (specifically helpers). So I shouldn't have to insert new configuration to be able to use them elsewhere.
My solution at the end of the day was to do this:
Rails.application.routes.url_helpers.athlete_url(athlete)
What I suspect, though I can't prove, is that including the url helpers manually with:
include Rails.application.routes.url_helpers
actually causes some included block to be executed. Doing so changes the configuration out from under the code trying to use it. By not including, but instead referring to it directly any 'included' blocks are not re-executed.
I'd love to know if anyone can explain if I'm on the right track or just making false assumptions.
Bottom line though, referring to the url_helpers with the full module/class path got the job done.

dynamically changing ActionMailer::Base.delivery_method

I need some of my emails to go using "smtp" and some others using "sendmail". Is there a clean way to switch between delivery methods depending on the mailer method?
You just need to define it with his configuration
ActionMailer::Base.delivery_method = :sendmail
And in other part of your app :
ActionMailer::Base.delivery_method = :smtp

rails 404 422 500 completely blank

This is probably very simple but I can't figure out why I get no error pages.
First off, I'm using Heroku for hosting, so it's definitely in production mode.
If I set the line "config.action_controller.consider_all_requests_local" set to true, I get a detailed error message but otherwise, I get a completely 100% blank screen. If I view the source, also blank.
All my 404,422,500.html files are in public and I haven't touched them.
And they seem to work on my local machine if I start in production mode there. So it must have to do with Heroku? Any ideas?
The logs tell me nothing useful.
Below is the details of the production.rb file
config.cache_classes = true
#ActionMailer::Base.delivery_method = :sendmail
Paperclip.options[:command_path] = "/usr/bin/"
# Don't care if the mailer can't send
config.action_mailer.raise_delivery_errors = false
# set delivery method to :smtp, :sendmail or :test
config.action_mailer.delivery_method = :smtp
# Full error reports are disabled and caching is turned on
config.action_controller.consider_all_requests_local = false
config.action_controller.perform_caching = true
config.action_view.cache_template_loading = true
I don't think Heroku uses the .html files like Passenger or Mongrel do. You may need to catch and handle your own exceptions through two basic mechanisms:
Create an exception handler with rescue_from in your ApplicationController for anything that might explode, both specifically and up to and including Object.
Create a default route to catch anything that isn't otherwise routed.
If you bust out of your routing table, or trigger a "500" error, it's because of exceptions. These need to be handled or you'll get a blank screen unless the web server is configured otherwise.
Apache can be configured to do this with the ErrorDocument directive.

Resources