Skip to content

Commit 90e8b7b

Browse files
committed
Merge branch 'add-test-mailbox-clear' into 4-0-maintenance
Merge #2293
2 parents 52d1665 + a597994 commit 90e8b7b

File tree

3 files changed

+30
-0
lines changed

3 files changed

+30
-0
lines changed

Changelog.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ Bug Fixes:
1818
(Jonathan Rochkind, #2242)
1919
* `rails generate generator` command now creates related spec file (Joel Azemar, #2217)
2020
* Relax upper `capybara` version constraint to allow for Capybara 3.x (Phil Pirozhkov, #2281)
21+
* Clear ActionMailer test mailbox after each example (Benoit Tigeot, #2293)
2122

2223
### 4.0.0.beta4
2324
[Full Changelog](https://github.com/rspec/rspec-rails/compare/v4.0.0.beta3...v4.0.0.beta4)

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: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -256,6 +256,34 @@ 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 'clears ActionMailer::Base::Deliveries after each example' do
261+
let(:mailer) do
262+
Class.new(ActionMailer::Base) do
263+
default from: '[email protected]'
264+
265+
def welcome(to:)
266+
mail(to: to, subject: 'subject', body: render(inline: "Hello", layout: false))
267+
end
268+
end
269+
end
270+
271+
before do
272+
ActionMailer::Base.delivery_method = :test
273+
end
274+
275+
it 'only has deliveries from this test (e.g. from [email protected])' do
276+
mailer.welcome(to: '[email protected]').deliver_now
277+
278+
expect(ActionMailer::Base.deliveries.map(&:to).flatten.sort).to eq(['[email protected]'])
279+
end
280+
281+
it 'only has deliveries from this test (e.g. from [email protected])' do
282+
mailer.welcome(to: '[email protected]').deliver_now
283+
284+
expect(ActionMailer::Base.deliveries.map(&:to).flatten.sort).to eq(['[email protected]'])
285+
end
286+
end
259287
end
260288

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

0 commit comments

Comments
 (0)