Blob Blame History Raw
From 50e2c7260ebe78e7ff765e5213f24068a2c40c28 Mon Sep 17 00:00:00 2001
From: Fabio Napoleoni <f.napoleoni@gmail.com>
Date: Fri, 10 Sep 2021 15:17:16 +0200
Subject: [PATCH 01/10] Change HaveEnqueuedEmail to work as usual in Rails 6

---
 .../rails/matchers/have_enqueued_mail_spec.rb  | 14 +++++++++++-------
 1 files changed, 6 insertions(+), 8 deletions(-)

diff --git a/spec/rspec/rails/matchers/have_enqueued_mail_spec.rb b/spec/rspec/rails/matchers/have_enqueued_mail_spec.rb
index 152e02fffc..119779352e 100644
--- a/spec/rspec/rails/matchers/have_enqueued_mail_spec.rb
+++ b/spec/rspec/rails/matchers/have_enqueued_mail_spec.rb
@@ -405,18 +405,16 @@ def self.name; "NonMailerJob"; end
         }.to have_enqueued_mail(UnifiedMailer, :email_with_args).with(1, 2)
       end
 
-      it "passes with provided argument matchers" do
+      it "matches arguments when mailer is parameterized" do
         expect {
           UnifiedMailer.with('foo' => 'bar').test_email.deliver_later
-        }.to have_enqueued_mail(UnifiedMailer, :test_email).with(
-          a_hash_including(params: {'foo' => 'bar'})
-        )
-
+        }.to have_enqueued_mail(UnifiedMailer, :test_email).with('foo' => 'bar')
+      end
+
+      it "matches arguments when mixing parameterized and non-parameterized emails" do
         expect {
           UnifiedMailer.with('foo' => 'bar').email_with_args(1, 2).deliver_later
-        }.to have_enqueued_mail(UnifiedMailer, :email_with_args).with(
-          a_hash_including(params: {'foo' => 'bar'}, args: [1, 2])
-        )
+        }.to have_enqueued_mail(UnifiedMailer, :email_with_args).with({'foo' => 'bar'}, 1, 2)
       end
 
       it "passes when given a global id serialised argument" do

From 7adcef22b5634e66919bead2f9fa9e5c494e07ba Mon Sep 17 00:00:00 2001
From: Fabio Napoleoni <f.napoleoni@gmail.com>
Date: Fri, 10 Sep 2021 15:22:09 +0200
Subject: [PATCH 02/10] Add missing relish documentation for parameter matching

---
 .../have_enqueued_mail_matcher.feature        | 37 +++++++++++++++++++
 1 file changed, 37 insertions(+)

diff --git a/features/matchers/have_enqueued_mail_matcher.feature b/features/matchers/have_enqueued_mail_matcher.feature
index ab1ae50210..b05fe78e45 100644
--- a/features/matchers/have_enqueued_mail_matcher.feature
+++ b/features/matchers/have_enqueued_mail_matcher.feature
@@ -38,3 +38,40 @@ Feature: have_enqueued_mail matcher
       """
     When I run `rspec spec/mailers/user_mailer_spec.rb`
     Then the examples should all pass
+
+  Scenario: Checking mailer arguments
+    Given a file named "app/mailers/my_mailer.rb" with:
+      """ruby
+      class MyMailer < ApplicationMailer
+
+        def signup(user = nil)
+          @user = user
+
+          mail to: "to@example.org"
+        end
+      end
+      """
+    Given a file named "spec/mailers/my_mailer_spec.rb" with:
+      """ruby
+      require "rails_helper"
+
+      RSpec.describe MyMailer do
+        it "matches with enqueued mailer" do
+          ActiveJob::Base.queue_adapter = :test
+          # Works with plain args
+          expect {
+            MyMailer.signup('user').deliver_later
+          }.to have_enqueued_mail(MyMailer, :signup).with('user')
+          # Works with named parameters
+          expect {
+            MyMailer.with('foo' => 'bar').signup.deliver_later
+          }.to have_enqueued_mail(MyMailer, :signup).with('foo' => 'bar')
+          # Works also with both, named parameters match first argument
+          expect {
+            MyMailer.with('foo' => 'bar').signup('user').deliver_later
+          }.to have_enqueued_mail(MyMailer, :signup).with({'foo' => 'bar'}, 'user')
+        end
+      end
+      """
+    When I run `rspec spec/mailers/my_mailer_spec.rb`
+    Then the examples should all pass

From 84ac4f8ace86232a549fd46572ded7ca8d80ba65 Mon Sep 17 00:00:00 2001
From: Fabio Napoleoni <f.napoleoni@gmail.com>
Date: Mon, 22 Nov 2021 15:13:22 +0100
Subject: [PATCH 05/10] Split all syntax in different examples

---
 .../have_enqueued_mail_matcher.feature        | 56 ++++++++++++++++++-
 1 file changed, 53 insertions(+), 3 deletions(-)

diff --git a/features/matchers/have_enqueued_mail_matcher.feature b/features/matchers/have_enqueued_mail_matcher.feature
index b05fe78e45..2091920a4b 100644
--- a/features/matchers/have_enqueued_mail_matcher.feature
+++ b/features/matchers/have_enqueued_mail_matcher.feature
@@ -62,14 +62,64 @@ Feature: have_enqueued_mail matcher
           expect {
             MyMailer.signup('user').deliver_later
           }.to have_enqueued_mail(MyMailer, :signup).with('user')
+        end
+      end
+      """
+    When I run `rspec spec/mailers/my_mailer_spec.rb`
+    Then the examples should all pass
+
+  Scenario: Parameterize the mailer
+    Given a file named "app/mailers/my_mailer.rb" with:
+      """ruby
+      class MyMailer < ApplicationMailer
+
+        def signup(user = nil)
+          @user = user
+
+          mail to: "to@example.org"
+        end
+      end
+      """
+    Given a file named "spec/mailers/my_mailer_spec.rb" with:
+      """ruby
+      require "rails_helper"
+
+      RSpec.describe MyMailer do
+        it "matches with enqueued mailer" do
+          ActiveJob::Base.queue_adapter = :test
           # Works with named parameters
           expect {
-            MyMailer.with('foo' => 'bar').signup.deliver_later
-          }.to have_enqueued_mail(MyMailer, :signup).with('foo' => 'bar')
+            MyMailer.with(foo: 'bar').signup.deliver_later
+          }.to have_enqueued_mail(MyMailer, :signup).with(foo: 'bar')
+        end
+      end
+      """
+    When I run `rspec spec/mailers/my_mailer_spec.rb`
+    Then the examples should all pass
+
+  Scenario: Parameterize and pass an argument to the mailer
+    Given a file named "app/mailers/my_mailer.rb" with:
+      """ruby
+      class MyMailer < ApplicationMailer
+
+        def signup(user = nil)
+          @user = user
+
+          mail to: "to@example.org"
+        end
+      end
+      """
+    Given a file named "spec/mailers/my_mailer_spec.rb" with:
+      """ruby
+      require "rails_helper"
+
+      RSpec.describe MyMailer do
+        it "matches with enqueued mailer" do
+          ActiveJob::Base.queue_adapter = :test
           # Works also with both, named parameters match first argument
           expect {
             MyMailer.with('foo' => 'bar').signup('user').deliver_later
-          }.to have_enqueued_mail(MyMailer, :signup).with({'foo' => 'bar'}, 'user')
+          }.to have_enqueued_mail(MyMailer, :signup).with({foo: 'bar'}, 'user')
         end
       end
       """

From 08d92ceb91c61bfd8cc7f932df3b31411eca9dd0 Mon Sep 17 00:00:00 2001
From: Fabio Napoleoni <f.napoleoni@gmail.com>
Date: Tue, 23 Nov 2021 10:19:23 +0100
Subject: [PATCH 06/10] Clarify examples with better params usage

---
 features/matchers/have_enqueued_mail_matcher.feature | 9 +++++----
 1 file changed, 5 insertions(+), 4 deletions(-)

diff --git a/features/matchers/have_enqueued_mail_matcher.feature b/features/matchers/have_enqueued_mail_matcher.feature
index 2091920a4b..8def19cf3c 100644
--- a/features/matchers/have_enqueued_mail_matcher.feature
+++ b/features/matchers/have_enqueued_mail_matcher.feature
@@ -73,8 +73,8 @@ Feature: have_enqueued_mail matcher
       """ruby
       class MyMailer < ApplicationMailer
 
-        def signup(user = nil)
-          @user = user
+        def signup
+          @foo = params[:foo]
 
           mail to: "to@example.org"
         end
@@ -102,8 +102,9 @@ Feature: have_enqueued_mail matcher
       """ruby
       class MyMailer < ApplicationMailer
 
-        def signup(user = nil)
+        def signup(user)
           @user = user
+          @foo = params[:foo]
 
           mail to: "to@example.org"
         end
@@ -118,7 +119,7 @@ Feature: have_enqueued_mail matcher
           ActiveJob::Base.queue_adapter = :test
           # Works also with both, named parameters match first argument
           expect {
-            MyMailer.with('foo' => 'bar').signup('user').deliver_later
+            MyMailer.with(foo: 'bar').signup('user').deliver_later
           }.to have_enqueued_mail(MyMailer, :signup).with({foo: 'bar'}, 'user')
         end
       end

From ed71aedc9fda010341b3cc296e075eff94496f63 Mon Sep 17 00:00:00 2001
From: mhenrixon <mikael@mhenrixon.com>
Date: Tue, 23 Nov 2021 20:59:48 +0100
Subject: [PATCH 07/10] Add compatibility for have_enqueued_mail (rails 7)

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
---
 spec/rspec/rails/matchers/have_enqueued_mail_spec.rb | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletions(-)

diff --git a/spec/rspec/rails/matchers/have_enqueued_mail_spec.rb b/spec/rspec/rails/matchers/have_enqueued_mail_spec.rb
index 119779352e..9413be1aa0 100644
--- a/spec/rspec/rails/matchers/have_enqueued_mail_spec.rb
+++ b/spec/rspec/rails/matchers/have_enqueued_mail_spec.rb
@@ -28,7 +28,12 @@ def test_email; end
       def email_with_args(arg1, arg2); end
     end
 
-    class DeliveryJobSubClass < ActionMailer::DeliveryJob
+    if RSpec::Rails::FeatureCheck.has_action_mailer_legacy_delivery_job?
+      class DeliveryJobSubClass < ActionMailer::DeliveryJob
+      end
+    else
+      class DeliveryJobSubClass < ActionMailer::MailDeliveryJob
+      end
     end
 
     class UnifiedMailerWithDeliveryJobSubClass < ActionMailer::Base

From a9cbac6c4949c40834ac5a13cca8c25d2f08e3cf Mon Sep 17 00:00:00 2001
From: Phil Pirozhkov <hello@fili.pp.ru>
Date: Tue, 21 Dec 2021 01:58:37 +0300
Subject: [PATCH 09/10] Fix system spec spec on Rails 7

ActionPack is checking `instance_created?` now, and it was `false`, so
`take_screenshot` was not called.

```
def take_failed_screenshot
  take_screenshot if failed? && supports_screenshot? && Capybara::Session.instance_created?
end
```

https://github.com/rails/rails/commit/1dfcffb583a89e1ef064db8dc6a45deaf157b147
---
 spec/rspec/rails/example/system_example_group_spec.rb | 1 +
 1 file changed, 1 insertion(+)

diff --git a/spec/rspec/rails/example/system_example_group_spec.rb b/spec/rspec/rails/example/system_example_group_spec.rb
index cd5328e2bf..cdc135946a 100644
--- a/spec/rspec/rails/example/system_example_group_spec.rb
+++ b/spec/rspec/rails/example/system_example_group_spec.rb
@@ -72,6 +72,7 @@ module RSpec::Rails
 
     describe '#after' do
       it 'sets the :extra_failure_lines metadata to an array of STDOUT lines' do
+        allow(Capybara::Session).to receive(:instance_created?).and_return(true)
         group = RSpec::Core::ExampleGroup.describe do
           include SystemExampleGroup