diff options
author | Yves Senn <yves.senn@gmail.com> | 2016-02-08 11:47:25 +0100 |
---|---|---|
committer | Yves Senn <yves.senn@gmail.com> | 2016-02-16 11:20:59 +0100 |
commit | 9d378747326d26cf1afdac4433ead22967af0984 (patch) | |
tree | bd377111a1ce085f8e31198fa636f6f9b8235254 /railties/test/application/integration_test_case_test.rb | |
parent | 3dd9fe5db44777eaf3765875fe259d53e72a0448 (diff) | |
download | rails-9d378747326d26cf1afdac4433ead22967af0984.tar.gz rails-9d378747326d26cf1afdac4433ead22967af0984.tar.bz2 rails-9d378747326d26cf1afdac4433ead22967af0984.zip |
reset `ActionMailer::Base.deliveries` in `ActionDispatch::IntegrationTest`.
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.
Diffstat (limited to 'railties/test/application/integration_test_case_test.rb')
-rw-r--r-- | railties/test/application/integration_test_case_test.rb | 46 |
1 files changed, 46 insertions, 0 deletions
diff --git a/railties/test/application/integration_test_case_test.rb b/railties/test/application/integration_test_case_test.rb new file mode 100644 index 0000000000..40a79fc636 --- /dev/null +++ b/railties/test/application/integration_test_case_test.rb @@ -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 |