Skip to content

Commit

Permalink
Clear test mailbox from ActionMailer::Base between each example
Browse files Browse the repository at this point in the history
We notice in #2290 that
ActionMailer::Base.deliveries mailbox is not cleaned between example.

Fix: #2290
  • Loading branch information
benoittgt committed Mar 20, 2020
1 parent 84e7925 commit 8eda2a1
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 0 deletions.
1 change: 1 addition & 0 deletions lib/rspec/rails/configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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?
Expand Down
25 changes: 25 additions & 0 deletions spec/rspec/rails/configuration_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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: '[email protected]'

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 protected]').deliver_now

expect(ActionMailer::Base.deliveries.map(&:to).flatten.sort).to eq(['[email protected]'])
end

it 'send to email_2@' do
BaseMailer.welcome(to: '[email protected]').deliver_now

expect(ActionMailer::Base.deliveries.map(&:to).flatten.sort).to eq(['[email protected]'])
end
end
end

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

0 comments on commit 8eda2a1

Please sign in to comment.