Skip to content

Commit 61454c5

Browse files
IstanfulJonRowe
authored andcommitted
Change negated active job matcher (#2069)
When expecting no job to be enqueued, the matcher would expect exactly one job not to have been enqueued. This commit changes so that it now expects no jobs to have been enqueued.
1 parent c92ce55 commit 61454c5

File tree

3 files changed

+31
-2
lines changed

3 files changed

+31
-2
lines changed

lib/rspec/rails/matchers/active_job.rb

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,12 @@ def matches?(proc)
197197

198198
check(in_block_jobs)
199199
end
200+
201+
def does_not_match?(proc)
202+
set_expected_number(:at_least, 1)
203+
204+
!matches?(proc)
205+
end
200206
end
201207

202208
# @private
@@ -205,6 +211,12 @@ def matches?(job)
205211
@job = job
206212
check(queue_adapter.enqueued_jobs)
207213
end
214+
215+
def does_not_match?(proc)
216+
set_expected_number(:at_least, 1)
217+
218+
!matches?(proc)
219+
end
208220
end
209221
end
210222

spec/rspec/rails/matchers/active_job_spec.rb

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,16 @@ def self.name; "LoggingJob"; end
122122
it "fails when negated and job is enqueued" do
123123
expect {
124124
expect { heavy_lifting_job.perform_later }.not_to have_enqueued_job
125-
}.to raise_error(/expected not to enqueue exactly 1 jobs, but enqueued 1/)
125+
}.to raise_error(/expected not to enqueue at least 1 jobs, but enqueued 1/)
126+
end
127+
128+
it "fails when negated and several jobs enqueued" do
129+
expect {
130+
expect {
131+
heavy_lifting_job.perform_later
132+
heavy_lifting_job.perform_later
133+
}.not_to have_enqueued_job
134+
}.to raise_error(/expected not to enqueue at least 1 jobs, but enqueued 2/)
126135
end
127136

128137
it "passes with job name" do
@@ -344,5 +353,13 @@ def self.name; "LoggingJob"; end
344353
expect(heavy_lifting_job).to have_been_enqueued
345354
}.to raise_error(/expected to enqueue exactly 1 jobs, but enqueued 0/)
346355
end
356+
357+
it "fails when negated and several jobs enqueued" do
358+
heavy_lifting_job.perform_later
359+
heavy_lifting_job.perform_later
360+
expect {
361+
expect(heavy_lifting_job).not_to have_been_enqueued
362+
}.to raise_error(/expected not to enqueue at least 1 jobs, but enqueued 2/)
363+
end
347364
end
348365
end

spec/rspec/rails/matchers/have_enqueued_mail_spec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ def email_with_optional_args(required_arg, optional_arg = nil); end
8989
expect {
9090
TestMailer.test_email.deliver_later
9191
}.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/)
92+
}.to raise_error(/expected not to enqueue TestMailer.test_email at least 1 time but enqueued 1/)
9393
end
9494

9595
it "passes with :once count" do

0 commit comments

Comments
 (0)