rspec-rails: uninitialized constant ActionMailer::DeliveryJob with latest rails/master

What Ruby, Rails and RSpec versions are you using?

Ruby version: 3.0.2 Rails version: 7.0.0 @ a0e14a8bfebf5f4bd4b66d1d468c844ab3e9d704 RSpec version: 5.0.2

Observed behaviour

6) Pitches::AcceptRequest#call updates pitch status
     Failure/Error:
       expect { call }.to change { User.count }.by(1)
         .and change { BuyerProfile.count }.by(1)
         .and change { pitch.reload.buyer_profile }.from(nil)
         .and change { prospect.reload.invitation_accepted_at }.from(nil)
         .and have_enqueued_email(PitchMailer, :request_accepted)

     NameError:
       uninitialized constant ActionMailer::DeliveryJob
       Did you mean?  ActionMailer::MailDeliveryJob
     # ./spec/services/pitches/accept_request_spec.rb:36:in `block (3 levels) in <main>'
     # -e:1:in `<main>'

Expected behaviour

Test was working before upgrading rails version

Can you provide an example app?

https://github.com/mhenrixon/rspec-rails-issue-2531 shows the error nicely

About this issue

  • Original URL
  • State: closed
  • Created 3 years ago
  • Comments: 19 (16 by maintainers)

Commits related to this issue

Most upvoted comments

Any chance of getting this in a tagged release soon?

This has been released as 6.0.0.rc1 theres still potential for the breaking change to the mailer api to do with params / args matching, but those needing support from a tagged release can now get this via rc1.

Rails 7 support is only via version 6.x per our versioning strategy.

I’m still getting the same error with rspec-rails 5.1.1 and rails 7.0.2.3

edit: Sweet, I see someone else gave this a 👍 , I felt like maybe I had done something incredibly wrong, and everyone else was good to go 😄

Added this in spec/support and it seems to work

# This patches rspec-rails to support rails 7.0
module RSpec
  module Rails
    module Matchers
      class HaveEnqueuedMail
        def legacy_mail?(job)
          defined?(ActionMailer::DeliveryJob) && job[:job] <= ActionMailer::DeliveryJob
        end

        def parameterized_mail?(job)
          RSpec::Rails::FeatureCheck.has_action_mailer_parameterized? && job[:job] <= ActionMailer::MailDeliveryJob
        end

        def unified_mail?(job)
          RSpec::Rails::FeatureCheck.has_action_mailer_unified_delivery? && job[:job] <= ActionMailer::MailDeliveryJob
        end
      end
    end
  end
end

What if you replace in https://github.com/rspec/rspec-rails/blob/d3e7b85877fcbcec63f8a76434d8750e7f3b7aef/lib/rspec/rails/matchers/have_enqueued_mail.rb#L134

-           job[:job] <= ActionMailer::DeliveryJob
+           defined?(ActionMailer::DeliveryJob) && job[:job] <= ActionMailer::DeliveryJob

will it fix your error?

The problem with main is that its version is 6.0.0-pre and other rspec libs (external) don’t yet support 6.x. Will this only be available in a 6.x release?

@Petercopter The fix is in main, but not in 5.1.1. I suggest using main until the fix is released on RubyGems.

Thanks @pirj feel free to close this one if #2546 fixes the issue! I am still swamped with my attempt to make our new home livable. Just catching up on some notifications.

Added PR @pirj, please let me know if there is anything I should improve to get it merged.

Perfect, I’ll sort out a PR then! Thanks for following up

You won’t even have to write new specs for this code. Rails edge build already fails:

NameError:
  uninitialized constant ActionMailer::DeliveryJob
# ./spec/rspec/rails/matchers/have_enqueued_mail_spec.rb:25:in `<top (required)>'