Permalink
Browse files

reset `ActionMailer::Base.deliveries` in `ActionDispatch::Integration…

…Test`.

Whenever you are sending emails in integration tests using the `:test`
delivery method you need to make sure that
`ActionMailer::Base.deliveries` is reset after every test. This piece of
boilerplate code is present in all my applications that send
emails. Let's have `ActionDispatch::IntegrationTest` reset the
deliveries automatically.
  • Loading branch information...
senny committed Feb 8, 2016
1 parent 3dd9fe5 commit 9d378747326d26cf1afdac4433ead22967af0984
@@ -1,3 +1,9 @@
* Reset `ActionMailer::Base.deliveries` after every test in
`ActionDispatch::IntegrationTest`.
*Yves Senn*
## Rails 5.0.0.beta2 (February 01, 2016) ##
* No changes.
@@ -51,6 +51,8 @@ class Railtie < Rails::Railtie # :nodoc:
get '/rails/mailers/*path' => "rails/mailers#preview"
end
end
ActionDispatch::IntegrationTest.send :include, ActionMailer::TestCase::ClearTestDeliviers

This comment has been minimized.

@senny

senny Feb 16, 2016

Member

@rafaelfranca I'm not sure this is the way to go to extend ActionDispatch::IntegrationTest outside of Action Pack. Let me know if this needs some form of adjustment.

end
end
@@ -11,6 +11,21 @@ def initialize(name)
end
class TestCase < ActiveSupport::TestCase
module ClearTestDeliviers
extend ActiveSupport::Concern
included do
teardown :clear_test_deliviers
end
private
def clear_test_deliviers
if ActionMailer::Base.delivery_method == :test
ActionMailer::Base.deliveries.clear
end
end
end
module Behavior
extend ActiveSupport::Concern
@@ -66,7 +81,6 @@ def initialize_test_deliveries # :nodoc:
def restore_test_deliveries # :nodoc:
restore_delivery_method
ActionMailer::Base.perform_deliveries = @old_perform_deliveries
ActionMailer::Base.deliveries.clear
end
def set_delivery_method(method) # :nodoc:
@@ -100,5 +114,6 @@ def read_fixture(action)
end
include Behavior
include ClearTestDeliviers

This comment has been minimized.

@kaspth

kaspth Feb 16, 2016

Member

@senny did you mean Deliveries? Otherwise 👍

This comment has been minimized.

@senny

senny Feb 16, 2016

Member

😓

This comment has been minimized.

@senny

senny Feb 16, 2016

Member

Thanks for the quick catch.

This comment has been minimized.

@senny

This comment has been minimized.

@kaspth

kaspth Feb 16, 2016

Member

❤️

end
end
@@ -1191,9 +1191,9 @@ testing) but instead it will be appended to an array
(`ActionMailer::Base.deliveries`).
NOTE: The `ActionMailer::Base.deliveries` array is only reset automatically in
`ActionMailer::TestCase` tests. If you want to have a clean slate outside Action
Mailer tests, you can reset it manually with:
`ActionMailer::Base.deliveries.clear`
`ActionMailer::TestCase` and `ActionDispatch::IntegrationTest` tests.
If you want to have a clean slate outside these test cases, you can reset it
manually with: `ActionMailer::Base.deliveries.clear`
### Functional Testing
@@ -0,0 +1,46 @@
require 'isolation/abstract_unit'
module ApplicationTests
class IntegrationTestCaseTest < ActiveSupport::TestCase
include ActiveSupport::Testing::Isolation
setup do
build_app
boot_rails
end
teardown do
teardown_app
end
test "resets Action Mailer test deliveries" do
script('generate mailer BaseMailer welcome')
app_file 'test/integration/mailer_integration_test.rb', <<-RUBY
require 'test_helper'
class MailerIntegrationTest < ActionDispatch::IntegrationTest
setup do
@old_delivery_method = ActionMailer::Base.delivery_method
ActionMailer::Base.delivery_method = :test
end
teardown do
ActionMailer::Base.delivery_method = @old_delivery_method
end
2.times do |i|
define_method "test_resets_deliveries_\#{i}" do
BaseMailer.welcome.deliver_now
assert_equal 1, ActionMailer::Base.deliveries.count
end
end
end
RUBY
output = Dir.chdir(app_path) { `bin/rails test 2>&1` }
assert_equal 0, $?.to_i, output
assert_match /0 failures, 0 errors/, output
end
end
end

2 comments on commit 9d37874

@senny

This comment has been minimized.

Member

senny replied Feb 16, 2016

@zzak this is what we discussed when working on the guides.

@vipulnsward

This comment has been minimized.

Member

vipulnsward replied Apr 15, 2016

👏

Please sign in to comment.