File tree 3 files changed +42
-3
lines changed
spec/rspec/rails/matchers
3 files changed +42
-3
lines changed Original file line number Diff line number Diff line change @@ -7,6 +7,8 @@ Bug Fixes:
7
7
(Eugene Kenny, Iliana, #2631 )
8
8
* Support Rails 7.1's ` #fixtures_paths ` in example groups (removes a deprecation warning).
9
9
(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 )
10
12
11
13
### 6.0.1 / 2022-10-18
12
14
[ 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)
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 = Set . new ( queue_adapter . enqueued_jobs )
234
234
proc . call
235
- in_block_jobs = queue_adapter . enqueued_jobs . drop ( original_enqueued_jobs_count )
235
+ enqueued_jobs = Set . new ( queue_adapter . enqueued_jobs )
236
236
237
- check ( in_block_jobs )
237
+ check ( enqueued_jobs - original_enqueued_jobs )
238
238
end
239
239
240
240
def does_not_match? ( proc )
Original file line number Diff line number Diff line change @@ -98,6 +98,43 @@ def self.name; "LoggingJob"; end
98
98
expect { } . not_to have_enqueued_job
99
99
end
100
100
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
+
101
138
it "fails when job is not enqueued" do
102
139
expect {
103
140
expect { } . to have_enqueued_job
You can’t perform that action at this time.
0 commit comments