Skip to content

Commit 8848e6a

Browse files
pirjJonRowe
authored andcommitted
Merge pull request #2573 from ojab/fixup_have_enqueued_job_on_retries
Fixup `have_enqueued_job` matcher on job retries
1 parent 98e3e39 commit 8848e6a

File tree

3 files changed

+42
-3
lines changed

3 files changed

+42
-3
lines changed

Changelog.md

+2
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ Bug Fixes:
77
(Eugene Kenny, Iliana, #2631)
88
* Support Rails 7.1's `#fixtures_paths` in example groups (removes a deprecation warning).
99
(Nicholas Simmons, #2664)
10+
* Fix `have_enqueued_job` to properly detect enqueued jobs when other jobs were
11+
performed inside the expectation block. (Slava Kardakov, Phil Pirozhkov, #2573)
1012

1113
### 6.0.1 / 2022-10-18
1214
[Full Changelog](https://github.com/rspec/rspec-rails/compare/v6.0.0...v6.0.1)

lib/rspec/rails/matchers/active_job.rb

+3-3
Original file line numberDiff line numberDiff line change
@@ -230,11 +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_count = queue_adapter.enqueued_jobs.count
233+
original_enqueued_jobs = Set.new(queue_adapter.enqueued_jobs)
234234
proc.call
235-
in_block_jobs = queue_adapter.enqueued_jobs.drop(original_enqueued_jobs_count)
235+
enqueued_jobs = Set.new(queue_adapter.enqueued_jobs)
236236

237-
check(in_block_jobs)
237+
check(enqueued_jobs - original_enqueued_jobs)
238238
end
239239

240240
def does_not_match?(proc)

spec/rspec/rails/matchers/active_job_spec.rb

+37
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,43 @@ def self.name; "LoggingJob"; end
9898
expect { }.not_to have_enqueued_job
9999
end
100100

101+
context "when previously enqueued jobs were performed" do
102+
include ActiveJob::TestHelper
103+
104+
before { stub_const("HeavyLiftingJob", heavy_lifting_job) }
105+
106+
it "counts newly enqueued jobs" do
107+
heavy_lifting_job.perform_later
108+
expect {
109+
perform_enqueued_jobs
110+
hello_job.perform_later
111+
}.to have_enqueued_job(hello_job)
112+
end
113+
end
114+
115+
context "when job is retried" do
116+
include ActiveJob::TestHelper
117+
118+
let(:unreliable_job) do
119+
Class.new(ActiveJob::Base) do
120+
retry_on StandardError, wait: 5, queue: :retry
121+
122+
def self.name; "UnreliableJob"; end
123+
def perform; raise StandardError; end
124+
end
125+
end
126+
127+
before { stub_const("UnreliableJob", unreliable_job) }
128+
129+
it "passes with reenqueued job" do
130+
time = Time.current.change(usec: 0)
131+
travel_to time do
132+
UnreliableJob.perform_later
133+
expect { perform_enqueued_jobs }.to have_enqueued_job(UnreliableJob).on_queue(:retry).at(time + 5)
134+
end
135+
end
136+
end
137+
101138
it "fails when job is not enqueued" do
102139
expect {
103140
expect { }.to have_enqueued_job

0 commit comments

Comments
 (0)