Skip to content

Commit 3fdd967

Browse files
committed
Define the mailer inside the spec with let
1 parent da95e4b commit 3fdd967

File tree

4 files changed

+62
-61
lines changed

4 files changed

+62
-61
lines changed

spec/rspec/rails/matchers/have_enqueued_mail_spec.rb

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,45 @@
33
if RSpec::Rails::FeatureCheck.has_active_job?
44
require "action_mailer"
55
require "rspec/rails/matchers/have_enqueued_mail"
6+
7+
class GlobalIDArgument
8+
include GlobalID::Identification
9+
def id; 1; end
10+
def to_global_id(options = {}); super(options.merge(app: 'rspec-rails')); end
11+
end
12+
13+
class TestMailer < ActionMailer::Base
14+
def test_email; end
15+
def email_with_args(arg1, arg2); end
16+
def email_with_optional_args(required_arg, optional_arg = nil); end
17+
end
18+
19+
class AnotherTestMailer < ActionMailer::Base
20+
def test_email; end
21+
end
22+
23+
if RSpec::Rails::FeatureCheck.has_action_mailer_unified_delivery?
24+
class UnifiedMailer < ActionMailer::Base
25+
self.delivery_job = ActionMailer::MailDeliveryJob
26+
27+
def test_email; end
28+
def email_with_args(arg1, arg2); end
29+
end
30+
31+
if RSpec::Rails::FeatureCheck.has_action_mailer_legacy_delivery_job?
32+
class DeliveryJobSubClass < ActionMailer::DeliveryJob
33+
end
34+
else
35+
class DeliveryJobSubClass < ActionMailer::MailDeliveryJob
36+
end
37+
end
38+
39+
class UnifiedMailerWithDeliveryJobSubClass < ActionMailer::Base
40+
self.delivery_job = DeliveryJobSubClass
41+
42+
def test_email; end
43+
end
44+
end
645
end
746

847
RSpec.describe "HaveEnqueuedMail matchers", skip: !RSpec::Rails::FeatureCheck.has_active_job? do

spec/rspec/rails/matchers/send_email_spec.rb

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,26 @@
11
# frozen_string_literal: true
22

33
RSpec.describe "send_email" do
4+
let(:mailer) do
5+
Class.new(ActionMailer::Base) do
6+
self.delivery_method = :test
7+
8+
def test_email
9+
mail(
10+
11+
12+
13+
14+
subject: "Test email",
15+
body: "Test email body"
16+
)
17+
end
18+
end
19+
end
20+
421
it "checks email sending by all params together" do
522
expect {
6-
TestMailer.test_email.deliver_now
23+
mailer.test_email.deliver_now
724
}.to send_email(
825
926
@@ -16,21 +33,21 @@
1633

1734
it "checks email sending by no params" do
1835
expect {
19-
TestMailer.test_email.deliver_now
36+
mailer.test_email.deliver_now
2037
}.to send_email
2138
end
2239

2340
it "has a negated version" do
2441
expect {
25-
TestMailer.test_email.deliver_now
42+
mailer.test_email.deliver_now
2643
}.to_not send_email(
2744
2845
)
2946
end
3047

3148
it "fails with a clear message" do
3249
expect {
33-
expect { TestMailer.test_email.deliver_now }.to send_email(from: '[email protected]')
50+
expect { mailer.test_email.deliver_now }.to send_email(from: '[email protected]')
3451
}.to raise_error(RSpec::Expectations::ExpectationNotMetError, <<~MSG.strip)
3552
No matching emails were sent.
3653
@@ -51,13 +68,13 @@
5168

5269
it "fails with a clear message for negated version" do
5370
expect {
54-
expect { TestMailer.test_email.deliver_now }.to_not send_email(from: "[email protected]")
71+
expect { mailer.test_email.deliver_now }.to_not send_email(from: "[email protected]")
5572
}.to raise_error(RSpec::Expectations::ExpectationNotMetError, "Expected not to send an email but it was sent.")
5673
end
5774

5875
it "fails for multiple matches" do
5976
expect {
60-
expect { 2.times { TestMailer.test_email.deliver_now } }.to send_email(from: "[email protected]")
77+
expect { 2.times { mailer.test_email.deliver_now } }.to send_email(from: "[email protected]")
6178
}.to raise_error(RSpec::Expectations::ExpectationNotMetError, <<~MSG.strip)
6279
More than 1 matching emails were sent.
6380

spec/spec_helper.rb

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,6 @@ def self.world=(world)
2020
end
2121
end
2222

23-
ActionMailer::Base.delivery_method = :test
24-
2523
I18n.enforce_available_locales = true
2624

2725
require 'rspec/support/spec'

spec/support/mailers.rb

Lines changed: 0 additions & 53 deletions
This file was deleted.

0 commit comments

Comments
 (0)