From 96690f66cc8aa8724f0defdaff52e6f68464d3be Mon Sep 17 00:00:00 2001 From: jbranchaud Date: Fri, 12 Apr 2019 14:28:41 -0500 Subject: [PATCH] Add Make ActionMailer Synchronous In Test as a rails til --- README.md | 3 +- .../make-action-mailer-synchronous-in-test.md | 31 +++++++++++++++++++ 2 files changed, 33 insertions(+), 1 deletion(-) create mode 100644 rails/make-action-mailer-synchronous-in-test.md diff --git a/README.md b/README.md index 8af8113..f2f4d38 100644 --- a/README.md +++ b/README.md @@ -10,7 +10,7 @@ smart people at [Hashrocket](http://hashrocket.com/). For a steady stream of TILs from a variety of rocketeers, checkout [til.hashrocket.com](https://til.hashrocket.com/). -_803 TILs and counting..._ +_804 TILs and counting..._ --- @@ -495,6 +495,7 @@ _803 TILs and counting..._ - [Hash Slicing](rails/hash-slicing.md) - [Ignore Poltergeist JavaScript Errors](rails/ignore-poltergeist-javascript-errors.md) - [List The Enqueued Jobs](rails/list-the-enqueued-jobs.md) +- [Make ActionMailer Synchronous In Test](rails/make-action-mailer-synchronous-in-test.md) - [Mark For Destruction](rails/mark-for-destruction.md) - [Migrating Up Down Up](rails/migrating-up-down-up.md) - [Params Includes Submission Button Info](rails/params-includes-submission-button-info.md) diff --git a/rails/make-action-mailer-synchronous-in-test.md b/rails/make-action-mailer-synchronous-in-test.md new file mode 100644 index 0000000..bd36846 --- /dev/null +++ b/rails/make-action-mailer-synchronous-in-test.md @@ -0,0 +1,31 @@ +# Make ActionMailer Synchronous In Test + +When you set up an `ActionMailer` email, the default configuration is for it +to use `ActiveJob` to send the emails. [As of Rails 5, it will do so +asynchronously.](https://blog.bigbinary.com/2016/03/29/rails-5-changed-default-active-job-adapter-to-async.html). +Depending on your preferences for testing emails, you may prefer `ActiveJob` +to send the emails synchronously. This can be done by changing the +`queue_adapter` back to `:inline` in your `config/environments/test.rb`. + +```ruby +config.active_job.queue_adapter = :inline +``` + +If you also configure the `delivery_method` as `:test`: + +```ruby +config.action_mailer.delivery_method = :test +``` + +then emails will be queued up in `ActionMailer::Base.deliveries` allowing +you to write a test like this: + +```ruby +expect(ActionMailer::Base.deliveries.count).to eq(1) +``` + +Check out [the +docs](https://guides.rubyonrails.org/action_mailer_basics.html) for more on +`ActionMailer`. + +[source](https://stackoverflow.com/a/42987726/535590)