Skip to content

uninitialized constant ActionMailer::DeliveryJob with latest rails/master #2531

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

Closed
mhenrixon opened this issue Nov 18, 2021 · 19 comments
Closed
Labels

Comments

@mhenrixon
Copy link
Contributor

mhenrixon commented Nov 18, 2021

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

@mhenrixon
Copy link
Contributor Author

mhenrixon commented Nov 18, 2021

Already fixed in #2516 actually no, that was not the same issue. I didn't have any issues with rspec-rails until the commit in rails master this morning.

@mhenrixon mhenrixon reopened this Nov 18, 2021
@mhenrixon
Copy link
Contributor Author

If I define the missing const

unless ActionMailer.const_defined?("DeliveryJob")
  ActionMailer.const_set(:DeliveryJob, ActionMailer::MailDeliveryJob)
end

I get other errors:

 4) Pitches::AcceptPitch#call when buyer has not connected with stripe updates pitch status
     Failure/Error:
       expect { call }.to change { pitch.reload.state }.to("buyer_accepted")
         .and have_enqueued_email(PitchMailer, :request_accepted)

     NameError:
       uninitialized constant ActionMailer::Parameterized::DeliveryJob
       Did you mean?  ActionMailer::DeliveryJob

So it does seem like something has changed and rspec-rails needs to be tweaked.

@pirj pirj added the rails7 label Nov 18, 2021
@pirj
Copy link
Member

pirj commented Nov 18, 2021

What if you replace in

job[:job] <= ActionMailer::DeliveryJob

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

will it fix your error?

@mhenrixon
Copy link
Contributor Author

@pirj then I get the other error I posted:

Failures:

  1) Article sends emails about published articles
     Failure/Error:
       expect { Article.create!(title: "Some title", slug: "some-title") }
         .to have_enqueued_email(ArticleMailer, :article_published)

     NameError:
       uninitialized constant ActionMailer::Parameterized::DeliveryJob
       Did you mean?  ActionMailer::MailDeliveryJob
     # ./spec/models/article_spec.rb:7:in `block (2 levels) in <main>'

@mhenrixon
Copy link
Contributor Author

This seems to be the commit! rails/rails@ddc7fb6

@mhenrixon
Copy link
Contributor Author

If I change:

# FROM
def parameterized_mail?(job)
  RSpec::Rails::FeatureCheck.has_action_mailer_parameterized? && job[:job] <= ActionMailer::Parameterized::DeliveryJob
end

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

At least the error goes away.

@mhenrixon
Copy link
Contributor Author

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

@pirj
Copy link
Member

pirj commented Nov 18, 2021

Would you open a PR with a proper fix that would work with 5.2, 6.0, 6.1 and most recent 7.0?

@pirj
Copy link
Member

pirj commented Nov 21, 2021

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)>'

@mhenrixon
Copy link
Contributor Author

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

pirj added a commit that referenced this issue Nov 23, 2021
mhenrixon added a commit to mhenrixon/rspec-rails that referenced this issue Nov 23, 2021
1. ActionMailer::DeliveryJob does not exist anymore
2. ActionMailer::Parameterized::DeliveryJob

According to my research they have both been superseeded by ActionMailer::MailDeliveryJob

Closes rspec#2531
@mhenrixon
Copy link
Contributor Author

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

mhenrixon added a commit to mhenrixon/rspec-rails that referenced this issue Nov 23, 2021
1. ActionMailer::DeliveryJob does not exist anymore
2. ActionMailer::Parameterized::DeliveryJob

According to my research they have both been superseeded by ActionMailer::MailDeliveryJob

Closes rspec#2531
mhenrixon added a commit to mhenrixon/rspec-rails that referenced this issue Nov 23, 2021
1. ActionMailer::DeliveryJob does not exist anymore
2. ActionMailer::Parameterized::DeliveryJob

According to my research they have both been superseeded by ActionMailer::MailDeliveryJob

Closes rspec#2531
pirj pushed a commit that referenced this issue Dec 20, 2021
1. ActionMailer::DeliveryJob does not exist anymore
2. ActionMailer::Parameterized::DeliveryJob

According to my research they have both been superseeded by ActionMailer::MailDeliveryJob

Closes #2531
@pirj
Copy link
Member

pirj commented Dec 20, 2021

It seems that specs were failing due to broken argument matching. It was mostly fixed in #2516, and with an additional small fix and this PR it all passes specs. A combo frankenrequest is #2546.

@mhenrixon
Copy link
Contributor Author

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.

pirj pushed a commit that referenced this issue Jan 10, 2022
1. ActionMailer::DeliveryJob does not exist anymore
2. ActionMailer::Parameterized::DeliveryJob

According to my research they have both been superseeded by ActionMailer::MailDeliveryJob

Closes #2531
@pirj pirj closed this as completed in bfab5ed Jan 10, 2022
@dwightwatson
Copy link

Any chance of getting this in a tagged release soon?

@Petercopter
Copy link

Petercopter commented Apr 2, 2022

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 :D

@pirj
Copy link
Member

pirj commented Apr 3, 2022

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

@rickychilcott
Copy link

rickychilcott commented Apr 3, 2022

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?

@pirj
Copy link
Member

pirj commented Apr 3, 2022

other rspec libs (external) don't yet support 6.x

Other RSpec libs do not depend on rspec-rails. Do you mean that rspec-rails's main branch depends on pre-release versions of other RSpec libs? Yes, this is true. You'll have to specify to use them from GitHub source, too.

Will this only be available in a 6.x release?

Yes, as this is a Rails 7 specific fix, and the version to support Rails 7 is RSpec Rails 6.

@JonRowe
Copy link
Member

JonRowe commented Apr 3, 2022

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.

jeppester added a commit to abtion/rails-template that referenced this issue May 4, 2022
To prevent possible issues with ActiveMailer:
rspec/rspec-rails#2531
thomasleese added a commit to DFE-Digital/apply-for-qualified-teacher-status that referenced this issue Aug 16, 2022
To prevent possible issues with ActiveMailer:
rspec/rspec-rails#2531
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants