File tree Expand file tree Collapse file tree 2 files changed +43
-3
lines changed
spec/rspec/rails/matchers Expand file tree Collapse file tree 2 files changed +43
-3
lines changed Original file line number Diff line number Diff line change @@ -230,11 +230,26 @@ def initialize(job)
230
230
def matches? ( proc )
231
231
raise ArgumentError , "have_enqueued_job and enqueue_job only support block expectations" unless Proc === proc
232
232
233
- original_enqueued_jobs_count = queue_adapter . enqueued_jobs . count
233
+ original_enqueued_jobs_hashes = queue_adapter . enqueued_jobs . map ( &:hash )
234
+
234
235
proc . call
235
- in_block_jobs = queue_adapter . enqueued_jobs . drop ( original_enqueued_jobs_count )
236
236
237
- check ( in_block_jobs )
237
+ in_block_jobs = queue_adapter . enqueued_jobs . each_with_object ( { } ) do |job , memo |
238
+ memo [ job . hash ] ||= { job : job , count : 0 }
239
+ memo [ 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 )
238
253
end
239
254
240
255
def does_not_match? ( proc )
Original file line number Diff line number Diff line change @@ -98,6 +98,31 @@ def self.name; "LoggingJob"; end
98
98
expect { } . not_to have_enqueued_job
99
99
end
100
100
101
+ context "when job is retried" do
102
+ include ActiveJob ::TestHelper
103
+
104
+ let ( :retried_job ) do
105
+ Class . new ( ActiveJob ::Base ) do
106
+ retry_on StandardError , wait : 5 , queue : :retry
107
+
108
+ def self . name ; "RetriedJob" ; end
109
+ def perform ; raise StandardError ; end
110
+ end
111
+ end
112
+
113
+ before do
114
+ stub_const ( "RetriedJob" , retried_job )
115
+ queue_adapter . perform_enqueued_jobs = true
116
+ end
117
+
118
+ it "passes with reenqueued job" do
119
+ time = Time . current . change ( usec : 0 )
120
+ travel_to time do
121
+ expect { retried_job . perform_later } . to have_enqueued_job ( retried_job ) . on_queue ( :retry ) . at ( time + 5 )
122
+ end
123
+ end
124
+ end
125
+
101
126
it "fails when job is not enqueued" do
102
127
expect {
103
128
expect { } . to have_enqueued_job
You can’t perform that action at this time.
0 commit comments