@@ -70,6 +70,28 @@ def email_with_optional_args(required_arg, optional_arg = nil); end
70
70
} . to raise_error ( /expected to enqueue TestMailer.test_email exactly 1 time/ )
71
71
end
72
72
73
+ it "matches based on mailer class and method name" do
74
+ expect {
75
+ TestMailer . test_email . deliver_later
76
+ TestMailer . email_with_args ( 1 , 2 ) . deliver_later
77
+ } . to have_enqueued_mail ( TestMailer , :test_email ) . once
78
+ end
79
+
80
+ it "passes with multiple emails" do
81
+ expect {
82
+ TestMailer . test_email . deliver_later
83
+ TestMailer . email_with_args ( 1 , 2 ) . deliver_later
84
+ } . to have_enqueued_mail ( TestMailer , :test_email ) . and have_enqueued_mail ( TestMailer , :email_with_args )
85
+ end
86
+
87
+ it 'fails when negated and mail is enqueued' do
88
+ expect {
89
+ expect {
90
+ TestMailer . test_email . deliver_later
91
+ } . not_to have_enqueued_mail ( TestMailer , :test_email )
92
+ } . to raise_error ( /expected not to enqueue TestMailer.test_email exactly 1 time, but enqueued 1/ )
93
+ end
94
+
73
95
it "passes with :once count" do
74
96
expect {
75
97
TestMailer . test_email . deliver_later
@@ -91,18 +113,32 @@ def email_with_optional_args(required_arg, optional_arg = nil); end
91
113
} . to have_enqueued_mail ( TestMailer , :test_email ) . thrice
92
114
end
93
115
94
- it "matches based on mailer class and method name " do
116
+ it "passes with at_least when enqueued emails are over the limit " do
95
117
expect {
96
118
TestMailer . test_email . deliver_later
97
- TestMailer . email_with_args ( 1 , 2 ) . deliver_later
98
- } . to have_enqueued_mail ( TestMailer , :test_email ) . once
119
+ TestMailer . test_email . deliver_later
120
+ } . to have_enqueued_mail ( TestMailer , :test_email ) . at_least ( : once)
99
121
end
100
122
101
- it "passes with multiple emails" do
123
+ it "passes with at_most when enqueued emails are under the limit " do
102
124
expect {
103
125
TestMailer . test_email . deliver_later
104
- TestMailer . email_with_args ( 1 , 2 ) . deliver_later
105
- } . to have_enqueued_mail ( TestMailer , :test_email ) . and have_enqueued_mail ( TestMailer , :email_with_args )
126
+ } . to have_enqueued_mail ( TestMailer , :test_email ) . at_most ( :twice )
127
+ end
128
+
129
+ it "generates a failure message with at least hint" do
130
+ expect {
131
+ expect { } . to have_enqueued_mail ( TestMailer , :test_email ) . at_least ( :once )
132
+ } . to raise_error ( /expected to enqueue TestMailer.test_email at least 1 time, but enqueued 0/ )
133
+ end
134
+
135
+ it "generates a failure message with at most hint" do
136
+ expect {
137
+ expect {
138
+ TestMailer . test_email . deliver_later
139
+ TestMailer . test_email . deliver_later
140
+ } . to have_enqueued_mail ( TestMailer , :test_email ) . at_most ( :once )
141
+ } . to raise_error ( /expected to enqueue TestMailer.test_email at most 1 time, but enqueued 2/ )
106
142
end
107
143
108
144
it "passes for mailer methods that accept arguments when the provided argument matcher is not used" do
@@ -144,11 +180,11 @@ def email_with_optional_args(required_arg, optional_arg = nil); end
144
180
it "generates a failure message with arguments" do
145
181
expect {
146
182
expect { } . to have_enqueued_email ( TestMailer , :email_with_args ) . with ( 1 , 2 )
147
- } . to raise_error ( /expected to enqueue TestMailer.email_with_args exactly 1 time with \[ 1, 2\] but enqueued 0/ )
183
+ } . to raise_error ( /expected to enqueue TestMailer.email_with_args exactly 1 time with \[ 1, 2\] , but enqueued 0/ )
148
184
end
149
185
150
186
it "generates a failure message with unmatching enqueued mail jobs" do
151
- message = "expected to enqueue TestMailer.email_with_args exactly 1 time with [1, 2] but enqueued 0" + \
187
+ message = "expected to enqueue TestMailer.email_with_args exactly 1 time with [1, 2], but enqueued 0" + \
152
188
"\n Queued deliveries:" + \
153
189
"\n TestMailer.test_email" + \
154
190
"\n TestMailer.email_with_args with [3, 4]"
0 commit comments