Skip to content

Commit 96690f6

Browse files
committed
Add Make ActionMailer Synchronous In Test as a rails til
1 parent e639284 commit 96690f6

File tree

2 files changed

+33
-1
lines changed

2 files changed

+33
-1
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ smart people at [Hashrocket](http://hashrocket.com/).
1010
For a steady stream of TILs from a variety of rocketeers, checkout
1111
[til.hashrocket.com](https://til.hashrocket.com/).
1212

13-
_803 TILs and counting..._
13+
_804 TILs and counting..._
1414

1515
---
1616

@@ -495,6 +495,7 @@ _803 TILs and counting..._
495495
- [Hash Slicing](rails/hash-slicing.md)
496496
- [Ignore Poltergeist JavaScript Errors](rails/ignore-poltergeist-javascript-errors.md)
497497
- [List The Enqueued Jobs](rails/list-the-enqueued-jobs.md)
498+
- [Make ActionMailer Synchronous In Test](rails/make-action-mailer-synchronous-in-test.md)
498499
- [Mark For Destruction](rails/mark-for-destruction.md)
499500
- [Migrating Up Down Up](rails/migrating-up-down-up.md)
500501
- [Params Includes Submission Button Info](rails/params-includes-submission-button-info.md)
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# Make ActionMailer Synchronous In Test
2+
3+
When you set up an `ActionMailer` email, the default configuration is for it
4+
to use `ActiveJob` to send the emails. [As of Rails 5, it will do so
5+
asynchronously.](https://blog.bigbinary.com/2016/03/29/rails-5-changed-default-active-job-adapter-to-async.html).
6+
Depending on your preferences for testing emails, you may prefer `ActiveJob`
7+
to send the emails synchronously. This can be done by changing the
8+
`queue_adapter` back to `:inline` in your `config/environments/test.rb`.
9+
10+
```ruby
11+
config.active_job.queue_adapter = :inline
12+
```
13+
14+
If you also configure the `delivery_method` as `:test`:
15+
16+
```ruby
17+
config.action_mailer.delivery_method = :test
18+
```
19+
20+
then emails will be queued up in `ActionMailer::Base.deliveries` allowing
21+
you to write a test like this:
22+
23+
```ruby
24+
expect(ActionMailer::Base.deliveries.count).to eq(1)
25+
```
26+
27+
Check out [the
28+
docs](https://guides.rubyonrails.org/action_mailer_basics.html) for more on
29+
`ActionMailer`.
30+
31+
[source](https://stackoverflow.com/a/42987726/535590)

0 commit comments

Comments
 (0)