Skip to content

Commit fb996b7

Browse files
author
Povilas Jurcys
committed
Make sure that negative matcher fails on multiple enqueues of the same kind
1 parent b37717a commit fb996b7

File tree

2 files changed

+51
-4
lines changed

2 files changed

+51
-4
lines changed

lib/rspec/rails/matchers/active_job.rb

Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@ def initialize
1616
@queue = nil
1717
@at = nil
1818
@block = Proc.new {}
19-
set_expected_number(:exactly, 1)
19+
@using_default_expected_number = true
20+
silently_set_expected_number(:exactly, 1)
2021
end
2122

2223
def with(*args, &block)
@@ -78,7 +79,7 @@ def failure_message
7879
end
7980

8081
def failure_message_when_negated
81-
"expected not to enqueue #{base_message}"
82+
"expected not to enqueue #{base_negative_message}"
8283
end
8384

8485
def message_expectation_modifier
@@ -95,6 +96,10 @@ def supports_block_expectations?
9596

9697
private
9798

99+
def using_default_expected_number?
100+
@using_default_expected_number
101+
end
102+
98103
def check(jobs)
99104
@matching_jobs, @unmatching_jobs = jobs.partition do |job|
100105
if arguments_match?(job) && other_attributes_match?(job)
@@ -115,7 +120,19 @@ def check(jobs)
115120
end
116121

117122
def base_message
118-
"#{message_expectation_modifier} #{@expected_number} jobs,".tap do |msg|
123+
message_with_base_info("#{message_expectation_modifier} #{@expected_number} jobs,")
124+
end
125+
126+
def base_negative_message
127+
if @expectation_type == :exactly && @expected_number.zero?
128+
message_with_base_info("job,")
129+
else
130+
base_message
131+
end
132+
end
133+
134+
def message_with_base_info(intro_message)
135+
intro_message.dup.tap do |msg|
119136
msg << " with #{@args}," if @args.any?
120137
msg << " on queue #{@queue}," if @queue
121138
msg << " at #{@at}," if @at
@@ -156,6 +173,11 @@ def serialized_attributes
156173
end
157174

158175
def set_expected_number(relativity, count)
176+
@using_default_expected_number = false
177+
silently_set_expected_number(relativity, count)
178+
end
179+
180+
def silently_set_expected_number(relativity, count)
159181
@expectation_type = relativity
160182
@expected_number = case count
161183
when :once then 1
@@ -187,6 +209,15 @@ def matches?(proc)
187209

188210
check(in_block_jobs)
189211
end
212+
213+
def does_not_match?(proc)
214+
if using_default_expected_number?
215+
silently_set_expected_number(:exactly, 0)
216+
matches?(proc)
217+
else
218+
!matches?(proc)
219+
end
220+
end
190221
end
191222

192223
# @private

spec/rspec/rails/matchers/active_job_spec.rb

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ def self.name; "LoggingJob"; end
116116
it "fails when negated and job is enqueued" do
117117
expect {
118118
expect { heavy_lifting_job.perform_later }.not_to have_enqueued_job
119-
}.to raise_error(/expected not to enqueue exactly 1 jobs, but enqueued 1/)
119+
}.to raise_error(/expected not to enqueue job, but enqueued 1/)
120120
end
121121

122122
it "passes with job name" do
@@ -134,6 +134,22 @@ def self.name; "LoggingJob"; end
134134
}.to have_enqueued_job(hello_job).and have_enqueued_job(logging_job)
135135
end
136136

137+
it "passes with multiple jobs of the same kind and different expected count when negated" do
138+
expect {
139+
hello_job.perform_later
140+
hello_job.perform_later
141+
}.not_to have_enqueued_job.once
142+
end
143+
144+
it "fails with multiple same type jobs when negated" do
145+
expect {
146+
expect {
147+
hello_job.perform_later
148+
hello_job.perform_later
149+
}.not_to have_enqueued_job
150+
}.to raise_error(/expected not to enqueue job, but enqueued 2/)
151+
end
152+
137153
it "passes with :once count" do
138154
expect {
139155
hello_job.perform_later

0 commit comments

Comments
 (0)