Skip to content

Commit efa83dc

Browse files
committed
Simplify the have_enqueued_job implementation
1 parent ea072de commit efa83dc

File tree

2 files changed

+8
-25
lines changed

2 files changed

+8
-25
lines changed

lib/rspec/rails/matchers/active_job.rb

Lines changed: 3 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -230,26 +230,11 @@ def initialize(job)
230230
def matches?(proc)
231231
raise ArgumentError, "have_enqueued_job and enqueue_job only support block expectations" unless Proc === proc
232232

233-
original_enqueued_jobs_hashes = queue_adapter.enqueued_jobs.map(&:hash)
234-
233+
original_enqueued_jobs = Set.new(queue_adapter.enqueued_jobs)
235234
proc.call
235+
enqueued_jobs = Set.new(queue_adapter.enqueued_jobs)
236236

237-
in_block_jobs = queue_adapter.enqueued_jobs.each_with_object({}) do |job, jobs|
238-
jobs[job.hash] ||= { job: job, count: 0 }
239-
jobs[job.hash][:count] += 1
240-
end
241-
242-
original_enqueued_jobs_hashes.each do |job_hash|
243-
in_block_jobs[job_hash][:count] -= 1 if in_block_jobs.key?(job_hash)
244-
end
245-
246-
in_block_jobs = in_block_jobs.each_value.flat_map do |job_and_count|
247-
count, job = job_and_count.values_at(:count, :job)
248-
249-
Array.new(count, job) if count.positive?
250-
end
251-
252-
check(in_block_jobs.compact)
237+
check(enqueued_jobs - original_enqueued_jobs)
253238
end
254239

255240
def does_not_match?(proc)

spec/rspec/rails/matchers/active_job_spec.rb

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -101,24 +101,22 @@ def self.name; "LoggingJob"; end
101101
context "when job is retried" do
102102
include ActiveJob::TestHelper
103103

104-
let(:retried_job) do
104+
let(:unreliable_job) do
105105
Class.new(ActiveJob::Base) do
106106
retry_on StandardError, wait: 5, queue: :retry
107107

108-
def self.name; "RetriedJob"; end
108+
def self.name; "UnreliableJob"; end
109109
def perform; raise StandardError; end
110110
end
111111
end
112112

113-
before do
114-
stub_const("RetriedJob", retried_job)
115-
queue_adapter.perform_enqueued_jobs = true
116-
end
113+
before { stub_const("UnreliableJob", unreliable_job) }
117114

118115
it "passes with reenqueued job" do
119116
time = Time.current.change(usec: 0)
120117
travel_to time do
121-
expect { retried_job.perform_later }.to have_enqueued_job(retried_job).on_queue(:retry).at(time + 5)
118+
UnreliableJob.perform_later
119+
expect { perform_enqueued_jobs }.to have_enqueued_job(UnreliableJob).on_queue(:retry).at(time + 5)
122120
end
123121
end
124122
end

0 commit comments

Comments
 (0)