Skip to content

Commit 0e25b66

Browse files
committed
Add emails sent inspect
1 parent b4693d9 commit 0e25b66

File tree

2 files changed

+45
-7
lines changed

2 files changed

+45
-7
lines changed

lib/rspec/rails/matchers/send_email.rb

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ module Matchers
77
#
88
# @see RSpec::Rails::Matchers#send_email
99
class SendEmail < RSpec::Rails::Matchers::BaseMatcher
10+
INSPECT_EMAIL_ATTRIBUTES = %i[subject from to cc bcc].freeze
11+
1012
def initialize(criteria)
1113
@criteria = criteria
1214
end
@@ -39,11 +41,13 @@ def match_when_negated(block)
3941
end
4042

4143
def failure_message
42-
if multiple_match?
43-
'More than 1 matching emails were sent.'
44-
else
45-
'No matching emails were sent.'
46-
end
44+
result =
45+
if multiple_match?
46+
'More than 1 matching emails were sent.'
47+
else
48+
'No matching emails were sent.'
49+
end
50+
"#{result}#{sent_emails_message}"
4751
end
4852

4953
def failure_message_when_negated
@@ -85,6 +89,19 @@ def matched_email?(email)
8589
def multiple_match?
8690
@matched_emails.many?
8791
end
92+
93+
def sent_emails_message
94+
if @diff.empty?
95+
"\n\nNo emails were sent."
96+
else
97+
sent_emails =
98+
@diff.map do |email|
99+
inspected = INSPECT_EMAIL_ATTRIBUTES.map { |attr| "#{attr}: #{email.public_send(attr)}" }.join(", ")
100+
"- #{inspected}"
101+
end.join("\n")
102+
"\n\nThe following emails were sent:\n#{sent_emails}"
103+
end
104+
end
88105
end
89106

90107
# @api public

spec/rspec/rails/matchers/send_email_spec.rb

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,22 @@
2929
it "fails with a clear message" do
3030
expect {
3131
expect { TestMailer.test_email.deliver_now }.to send_email(from: '[email protected]')
32-
}.to raise_error(RSpec::Expectations::ExpectationNotMetError, "No matching emails were sent.")
32+
}.to raise_error(RSpec::Expectations::ExpectationNotMetError, <<~MSG.strip)
33+
No matching emails were sent.
34+
35+
The following emails were sent:
36+
- subject: Test email, from: ["[email protected]"], to: ["[email protected]"], cc: ["[email protected]"], bcc: ["[email protected]"]
37+
MSG
38+
end
39+
40+
it "fails with a clear message when no emails were sent" do
41+
expect {
42+
expect { }.to send_email
43+
}.to raise_error(RSpec::Expectations::ExpectationNotMetError, <<~MSG.strip)
44+
No matching emails were sent.
45+
46+
No emails were sent.
47+
MSG
3348
end
3449

3550
it "fails with a clear message for negated version" do
@@ -41,6 +56,12 @@
4156
it "fails for multiple matches" do
4257
expect {
4358
expect { 2.times { TestMailer.test_email.deliver_now } }.to send_email(from: '[email protected]')
44-
}.to raise_error(RSpec::Expectations::ExpectationNotMetError, "More than 1 matching emails were sent.")
59+
}.to raise_error(RSpec::Expectations::ExpectationNotMetError, <<~MSG.strip)
60+
More than 1 matching emails were sent.
61+
62+
The following emails were sent:
63+
- subject: Test email, from: ["[email protected]"], to: ["[email protected]"], cc: ["[email protected]"], bcc: ["[email protected]"]
64+
- subject: Test email, from: ["[email protected]"], to: ["[email protected]"], cc: ["[email protected]"], bcc: ["[email protected]"]
65+
MSG
4566
end
4667
end

0 commit comments

Comments
 (0)