rspec-rails: Possible regression 4.0.0.beta4 to 4.0.0.rc1: List of ActionMailer deliveries is not cleared between specs

What Ruby, Rails and RSpec versions are you using?

Ruby version: 2.7.0 Rails version: 6.0.2 RSpec version: 4.0.0.rc1

The following is included for these mailer tests:

RSpec.configure do |config| config.include ActionMailer::TestHelper end

Observed behaviour

When running specs, we check the to adresses that are set for the ActionMailer::Base.deliveries, like this:

expect(ActionMailer::Base.deliveries.map(&:to).flatten.include?(leo.email)).to eq true
expect(ActionMailer::Base.deliveries.map(&:to).flatten.sort).to eq([droid.email, leo.email])

This works for version 4.0.0.beta3 and 4.0.0.beta4, but for 4.0.0.rc1, this will break. It seems that the deliveries array is not cleared between specs. The list of email addresses that are stored in the TO of the mailer, will get larger after each spec. Therefore, running each spec seperately works, but when the fourth or fifth spec is ran, the list of adresses is way bigger than expected:

     expected: ["droid@cm.nl", "leo@cm.nl"]
            got: ["droid@cm.nl", "droid@cm.nl", "leo@cm.nl", "leo@cm.nl", "leo@hoefsmit.nl"]

Expected behaviour

The ActionMailer instance is reset between each spec and the TO, CC and BCC attributes of ActionMailer::Base.deliveries should be cleared between each spec.

Can you provide an example app?

I have isolated the issue into an example app: https://github.com/CUnknown/rspec-rails-bug-example

About this issue

  • Original URL
  • State: closed
  • Created 4 years ago
  • Reactions: 1
  • Comments: 20 (15 by maintainers)

Commits related to this issue

Most upvoted comments

 # lib/rspec/rails/configuration.rb:138
 if RSpec::Rails::FeatureCheck.has_action_mailer?
   config.include RSpec::Rails::MailerExampleGroup, type: :mailer
+  config.after { ActionMailer::Base.deliveries.clear }
 end