Skip to content

Commit 8eda2a1

Browse files
committed
Clear test mailbox from ActionMailer::Base between each example
We notice in #2290 that ActionMailer::Base.deliveries mailbox is not cleaned between example. Fix: #2290
1 parent 84e7925 commit 8eda2a1

File tree

2 files changed

+26
-0
lines changed

2 files changed

+26
-0
lines changed

lib/rspec/rails/configuration.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,7 @@ def filter_rails_from_backtrace!
137137

138138
if RSpec::Rails::FeatureCheck.has_action_mailer?
139139
config.include RSpec::Rails::MailerExampleGroup, type: :mailer
140+
config.after { ActionMailer::Base.deliveries.clear }
140141
end
141142

142143
if RSpec::Rails::FeatureCheck.has_active_job?

spec/rspec/rails/configuration_spec.rb

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -256,6 +256,31 @@ def self.application; end
256256
expect(group.mailer_class).to be(a_mailer_class)
257257
expect(group.new).to be_a(RSpec::Rails::MailerExampleGroup)
258258
end
259+
260+
describe 'cleans test mailbox between each example in all rspec-rails example' do
261+
class BaseMailer < ActionMailer::Base
262+
default from: '[email protected]'
263+
264+
def welcome(to:)
265+
mail(to: to, subject: 'subject', body: render(inline: "Hello", layout: false))
266+
end
267+
end
268+
before do
269+
ActionMailer::Base.delivery_method = :test
270+
end
271+
272+
it 'send to email@' do
273+
BaseMailer.welcome(to: '[email protected]').deliver_now
274+
275+
expect(ActionMailer::Base.deliveries.map(&:to).flatten.sort).to eq(['[email protected]'])
276+
end
277+
278+
it 'send to email_2@' do
279+
BaseMailer.welcome(to: '[email protected]').deliver_now
280+
281+
expect(ActionMailer::Base.deliveries.map(&:to).flatten.sort).to eq(['[email protected]'])
282+
end
283+
end
259284
end
260285

261286
it "has a default #file_fixture_path of 'spec/fixtures/files'" do

0 commit comments

Comments
 (0)