Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Clear test mailbox from ActionMailer::Base between each example #2293

Merged
merged 4 commits into from Mar 20, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions lib/rspec/rails/configuration.rb
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
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
JonRowe marked this conversation as resolved.
Show resolved Hide resolved
benoittgt marked this conversation as resolved.
Show resolved Hide resolved
class BaseMailer < ActionMailer::Base
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can this be an anonymous class in a let?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yep! Good idea

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you explain you prefer?

Done with 368484b

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
benoittgt marked this conversation as resolved.
Show resolved Hide resolved
end
end

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