From 5556f1b53b70899a9127a06877f7a4fb804ed4f7 Mon Sep 17 00:00:00 2001 From: Benoit Tigeot Date: Fri, 20 Mar 2020 18:29:47 +0100 Subject: [PATCH 1/6] Clear test mailbox from ActionMailer::Base between each example We notice in https://github.com/rspec/rspec-rails/issues/2290 that ActionMailer::Base.deliveries mailbox is not cleaned between example. Fix: https://github.com/rspec/rspec-rails/issues/2290 --- lib/rspec/rails/configuration.rb | 1 + spec/rspec/rails/configuration_spec.rb | 25 +++++++++++++++++++++++++ 2 files changed, 26 insertions(+) diff --git a/lib/rspec/rails/configuration.rb b/lib/rspec/rails/configuration.rb index 735ec2d9dd..4c0e789a6b 100644 --- a/lib/rspec/rails/configuration.rb +++ b/lib/rspec/rails/configuration.rb @@ -137,6 +137,7 @@ def filter_rails_from_backtrace! if RSpec::Rails::FeatureCheck.has_action_mailer? config.include RSpec::Rails::MailerExampleGroup, type: :mailer + config.after { ActionMailer::Base.deliveries.clear } end if RSpec::Rails::FeatureCheck.has_active_job? diff --git a/spec/rspec/rails/configuration_spec.rb b/spec/rspec/rails/configuration_spec.rb index 7187c67fc1..bbed979a45 100644 --- a/spec/rspec/rails/configuration_spec.rb +++ b/spec/rspec/rails/configuration_spec.rb @@ -256,6 +256,31 @@ def self.application; end expect(group.mailer_class).to be(a_mailer_class) expect(group.new).to be_a(RSpec::Rails::MailerExampleGroup) end + + describe 'cleans test mailbox between each example in all rspec-rails example' do + class BaseMailer < ActionMailer::Base + default from: 'from@example.com' + + def welcome(to:) + mail(to: to, subject: 'subject', body: render(inline: "Hello", layout: false)) + end + end + before do + ActionMailer::Base.delivery_method = :test + end + + it 'send to email@' do + BaseMailer.welcome(to: 'email@example.com').deliver_now + + expect(ActionMailer::Base.deliveries.map(&:to).flatten.sort).to eq(['email@example.com']) + end + + it 'send to email_2@' do + BaseMailer.welcome(to: 'email_2@example.com').deliver_now + + expect(ActionMailer::Base.deliveries.map(&:to).flatten.sort).to eq(['email_2@example.com']) + end + end end it "has a default #file_fixture_path of 'spec/fixtures/files'" do From dff5c2227a36538e6662cf1740a701b8e87ad712 Mon Sep 17 00:00:00 2001 From: Benoit Tigeot Date: Fri, 20 Mar 2020 22:04:18 +0100 Subject: [PATCH 2/6] More generic 'describe' description --- spec/rspec/rails/configuration_spec.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/spec/rspec/rails/configuration_spec.rb b/spec/rspec/rails/configuration_spec.rb index bbed979a45..9b54212cbb 100644 --- a/spec/rspec/rails/configuration_spec.rb +++ b/spec/rspec/rails/configuration_spec.rb @@ -257,7 +257,7 @@ def self.application; end expect(group.new).to be_a(RSpec::Rails::MailerExampleGroup) end - describe 'cleans test mailbox between each example in all rspec-rails example' do + describe 'leans test mailbox after each example' do class BaseMailer < ActionMailer::Base default from: 'from@example.com' From a7891f51a55f120f3322b26df2d63bb59909d710 Mon Sep 17 00:00:00 2001 From: Benoit Tigeot Date: Fri, 20 Mar 2020 22:08:08 +0100 Subject: [PATCH 3/6] Switch to an anonymous class instead --- spec/rspec/rails/configuration_spec.rb | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/spec/rspec/rails/configuration_spec.rb b/spec/rspec/rails/configuration_spec.rb index 9b54212cbb..1d7ce79576 100644 --- a/spec/rspec/rails/configuration_spec.rb +++ b/spec/rspec/rails/configuration_spec.rb @@ -258,25 +258,28 @@ def self.application; end end describe 'leans test mailbox after each example' do - class BaseMailer < ActionMailer::Base - default from: 'from@example.com' + let(:base_mailer) do + Class.new(ActionMailer::Base) do + default from: 'from@example.com' - def welcome(to:) - mail(to: to, subject: 'subject', body: render(inline: "Hello", layout: false)) + def welcome(to:) + mail(to: to, subject: 'subject', body: render(inline: "Hello", layout: false)) + end end end + before do ActionMailer::Base.delivery_method = :test end it 'send to email@' do - BaseMailer.welcome(to: 'email@example.com').deliver_now + base_mailer.welcome(to: 'email@example.com').deliver_now expect(ActionMailer::Base.deliveries.map(&:to).flatten.sort).to eq(['email@example.com']) end it 'send to email_2@' do - BaseMailer.welcome(to: 'email_2@example.com').deliver_now + base_mailer.welcome(to: 'email_2@example.com').deliver_now expect(ActionMailer::Base.deliveries.map(&:to).flatten.sort).to eq(['email_2@example.com']) end From 87354750f24778b5ac3e942bb1a7e790eb680bca Mon Sep 17 00:00:00 2001 From: Benoit Tigeot Date: Sat, 21 Mar 2020 00:02:11 +0100 Subject: [PATCH 4/6] Changelog for #2293 --- Changelog.md | 1 + 1 file changed, 1 insertion(+) diff --git a/Changelog.md b/Changelog.md index ad56f32881..5965a9b9f5 100644 --- a/Changelog.md +++ b/Changelog.md @@ -18,6 +18,7 @@ Bug Fixes: (Jonathan Rochkind, #2242) * `rails generate generator` command now creates related spec file (Joel Azemar, #2217) * Relax upper `capybara` version constraint to allow for Capybara 3.x (Phil Pirozhkov, #2281) +* Leans ActionMailer test mailbox after each example (Benoit Tigeot, #2293) ### 4.0.0.beta4 [Full Changelog](https://github.com/rspec/rspec-rails/compare/v4.0.0.beta3...v4.0.0.beta4) From ee99c1112d1c216f9a76c3caaa29a255c90ebc57 Mon Sep 17 00:00:00 2001 From: Jon Rowe Date: Sat, 21 Mar 2020 10:00:14 +0000 Subject: [PATCH 5/6] =?UTF-8?q?Typo=20=F0=9F=99=83?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Changelog.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Changelog.md b/Changelog.md index 5965a9b9f5..b06ca9c109 100644 --- a/Changelog.md +++ b/Changelog.md @@ -18,7 +18,7 @@ Bug Fixes: (Jonathan Rochkind, #2242) * `rails generate generator` command now creates related spec file (Joel Azemar, #2217) * Relax upper `capybara` version constraint to allow for Capybara 3.x (Phil Pirozhkov, #2281) -* Leans ActionMailer test mailbox after each example (Benoit Tigeot, #2293) +* Clear ActionMailer test mailbox after each example (Benoit Tigeot, #2293) ### 4.0.0.beta4 [Full Changelog](https://github.com/rspec/rspec-rails/compare/v4.0.0.beta3...v4.0.0.beta4) From a597994cac62e80f79b00a54a351738bd45f6625 Mon Sep 17 00:00:00 2001 From: Jon Rowe Date: Sat, 21 Mar 2020 20:53:16 +0000 Subject: [PATCH 6/6] Merge pull request #2295 from rspec/reword-specs Reword mailer specs doc strings for ensuring deliveries are cleared --- spec/rspec/rails/configuration_spec.rb | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/spec/rspec/rails/configuration_spec.rb b/spec/rspec/rails/configuration_spec.rb index 1d7ce79576..91379ff630 100644 --- a/spec/rspec/rails/configuration_spec.rb +++ b/spec/rspec/rails/configuration_spec.rb @@ -257,8 +257,8 @@ def self.application; end expect(group.new).to be_a(RSpec::Rails::MailerExampleGroup) end - describe 'leans test mailbox after each example' do - let(:base_mailer) do + describe 'clears ActionMailer::Base::Deliveries after each example' do + let(:mailer) do Class.new(ActionMailer::Base) do default from: 'from@example.com' @@ -272,14 +272,14 @@ def welcome(to:) ActionMailer::Base.delivery_method = :test end - it 'send to email@' do - base_mailer.welcome(to: 'email@example.com').deliver_now + it 'only has deliveries from this test (e.g. from email@example.com)' do + mailer.welcome(to: 'email@example.com').deliver_now expect(ActionMailer::Base.deliveries.map(&:to).flatten.sort).to eq(['email@example.com']) end - it 'send to email_2@' do - base_mailer.welcome(to: 'email_2@example.com').deliver_now + it 'only has deliveries from this test (e.g. from email_2@example.com)' do + mailer.welcome(to: 'email_2@example.com').deliver_now expect(ActionMailer::Base.deliveries.map(&:to).flatten.sort).to eq(['email_2@example.com']) end