Skip to content

Commit bc9e1d4

Browse files
committed
Add specs for mail delivery
1 parent bafd4e9 commit bc9e1d4

File tree

2 files changed

+47
-5
lines changed

2 files changed

+47
-5
lines changed

lib/supermail.rb

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,25 @@ module Supermail
88
class Error < StandardError; end
99

1010
module Rails
11+
# This is a bizzare work around for a commit that broke https://github.com/rails/rails/commit/c594ba4ffdb016c7b2a22055f41dfb2c4409594d
12+
# further proving the bewildering maze of indirection in Rails ActionMailer.
13+
class Mailer < ActionMailer::Base
14+
def mail(...)
15+
super(...)
16+
end
17+
18+
def self.message_delivery(**)
19+
ActionMailer::MessageDelivery.new self, :mail, **
20+
end
21+
end
22+
1123
class Base
1224
delegate \
1325
:deliver,
1426
:deliver_now,
1527
:deliver_later,
1628
:message,
17-
to: :action_mailer_message_delivery
29+
to: :message_delivery
1830

1931
def to = nil
2032
def from = nil
@@ -27,10 +39,8 @@ def body = ""
2739
def mailto = MailTo.href(to:, from:, cc:, bcc:, subject:, body:)
2840
alias :mail_to :mailto
2941

30-
# This is a bizzare work around for a commit that broke https://github.com/rails/rails/commit/c594ba4ffdb016c7b2a22055f41dfb2c4409594d
31-
# further proving the bewildering maze of indirection in Rails ActionMailer.
32-
private def action_mailer_message_delivery
33-
ActionMailer::MessageDelivery.new(ActionMailer::Base, :mail,
42+
private def message_delivery
43+
Rails::Mailer.message_delivery(
3444
to:,
3545
from:,
3646
cc:,

spec/supermail_spec.rb

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,4 +58,36 @@ def initialize(to:, from:, subject:, body:, cc: [], bcc: [])
5858
expect(result).to include("bcc=%5B%22bcc%40example.com%22%5D")
5959
end
6060
end
61+
62+
describe "delivery" do
63+
before do
64+
ActionMailer::Base.delivery_method = :test
65+
ActionMailer::Base.deliveries.clear
66+
ActiveJob::Base.queue_adapter = :test
67+
end
68+
69+
describe "#deliver_now" do
70+
it "delivers the email through ActionMailer" do
71+
expect {
72+
email.deliver_now
73+
}.to change { ActionMailer::Base.deliveries.count }.by(1)
74+
end
75+
end
76+
77+
describe "#deliver" do
78+
it "delivers the email through ActionMailer" do
79+
expect {
80+
email.deliver
81+
}.to change { ActionMailer::Base.deliveries.count }.by(1)
82+
end
83+
end
84+
85+
describe "#deliver_later" do
86+
it "enqueues the email for delivery through ActiveJob" do
87+
expect {
88+
email.deliver_later
89+
}.to change { ActiveJob::Base.queue_adapter.enqueued_jobs.size }.by(1)
90+
end
91+
end
92+
end
6193
end

0 commit comments

Comments
 (0)