|
1 |
| -# frozen_string_literal: true |
2 |
| - |
3 | 1 | RSpec.describe "send_email" do
|
4 | 2 | let(:mailer) do
|
5 | 3 | Class.new(ActionMailer::Base) do
|
@@ -37,7 +35,7 @@ def test_email
|
37 | 35 | }.to send_email
|
38 | 36 | end
|
39 | 37 |
|
40 |
| - it "has a negated version" do |
| 38 | + it "with with to_not" do |
41 | 39 | expect {
|
42 | 40 | mailer.test_email.deliver_now
|
43 | 41 | }.to_not send_email(
|
@@ -83,4 +81,88 @@ def test_email
|
83 | 81 |
|
84 | 82 | MSG
|
85 | 83 | end
|
| 84 | + |
| 85 | + context "with compound matching" do |
| 86 | + it "works when both matchings pass" do |
| 87 | + expect { |
| 88 | + expect { |
| 89 | + mailer.test_email.deliver_now |
| 90 | + }.to send_email(to: "[email protected]").and send_email(from: "[email protected]") |
| 91 | + }.to_not raise_error |
| 92 | + end |
| 93 | + |
| 94 | + it "works when first matching fails" do |
| 95 | + expect { |
| 96 | + expect { |
| 97 | + mailer.test_email.deliver_now |
| 98 | + }.to send_email(to: "[email protected]").and send_email(to: "[email protected]") |
| 99 | + }.to raise_error(RSpec::Expectations::ExpectationNotMetError, <<~MSG.strip) |
| 100 | + No matching emails were sent. |
| 101 | +
|
| 102 | + The following emails were sent: |
| 103 | + |
| 104 | + MSG |
| 105 | + end |
| 106 | + |
| 107 | + it "works when second matching fails" do |
| 108 | + expect { |
| 109 | + expect { |
| 110 | + mailer.test_email.deliver_now |
| 111 | + }.to send_email(to: "[email protected]").and send_email(to: "[email protected]") |
| 112 | + }.to raise_error(RSpec::Expectations::ExpectationNotMetError, <<~MSG.strip) |
| 113 | + No matching emails were sent. |
| 114 | +
|
| 115 | + The following emails were sent: |
| 116 | + |
| 117 | + MSG |
| 118 | + end |
| 119 | + end |
| 120 | + |
| 121 | + context "with a custom negated version defined" do |
| 122 | + before { RSpec::Matchers.define_negated_matcher :not_send_email, :send_email } |
| 123 | + |
| 124 | + it "works with a negated version" do |
| 125 | + expect { |
| 126 | + mailer.test_email.deliver_now |
| 127 | + }.to not_send_email( |
| 128 | + |
| 129 | + ) |
| 130 | + end |
| 131 | + |
| 132 | + it "fails with a clear message" do |
| 133 | + expect { |
| 134 | + expect { mailer.test_email.deliver_now }.to not_send_email(from: "[email protected]") |
| 135 | + }.to raise_error(RSpec::Expectations::ExpectationNotMetError, "Expected not to send an email but it was sent.") |
| 136 | + end |
| 137 | + |
| 138 | + context "with a compound negated version" do |
| 139 | + it "works when both matchings pass" do |
| 140 | + expect { |
| 141 | + expect { |
| 142 | + mailer.test_email.deliver_now |
| 143 | + }.to not_send_email(to: "[email protected]").and not_send_email(from: "[email protected]") |
| 144 | + }.to_not raise_error |
| 145 | + end |
| 146 | + |
| 147 | + it "works when first matching fails" do |
| 148 | + expect { |
| 149 | + expect { |
| 150 | + mailer.test_email.deliver_now |
| 151 | + }.to not_send_email(to: "[email protected]").and send_email(to: "[email protected]") |
| 152 | + }.to raise_error(RSpec::Expectations::ExpectationNotMetError, a_string_including(<<~MSG.strip)) |
| 153 | + Expected not to send an email but it was sent. |
| 154 | + MSG |
| 155 | + end |
| 156 | + |
| 157 | + it "works when second matching fails" do |
| 158 | + expect { |
| 159 | + expect { |
| 160 | + mailer.test_email.deliver_now |
| 161 | + }.to send_email(to: "[email protected]").and not_send_email(to: "[email protected]") |
| 162 | + }.to raise_error(RSpec::Expectations::ExpectationNotMetError, a_string_including(<<~MSG.strip)) |
| 163 | + Expected not to send an email but it was sent. |
| 164 | + MSG |
| 165 | + end |
| 166 | + end |
| 167 | + end |
86 | 168 | end
|
0 commit comments