File tree Expand file tree Collapse file tree 3 files changed +42
-3
lines changed
spec/rspec/rails/matchers Expand file tree Collapse file tree 3 files changed +42
-3
lines changed Original file line number Diff line number Diff 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 )
Original file line number Diff line number Diff 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 )
Original file line number Diff line number Diff 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
You can’t perform that action at this time.
0 commit comments