File tree 2 files changed +32
-1
lines changed
spec/rspec/rails/matchers
2 files changed +32
-1
lines changed Original file line number Diff line number Diff line change @@ -163,7 +163,27 @@ def at_match?(job)
163
163
return job [ :at ] . nil? if @at == :no_wait
164
164
return false unless job [ :at ]
165
165
166
- values_match? ( @at , Time . at ( job [ :at ] ) )
166
+ scheduled_at = Time . at ( job [ :at ] )
167
+ values_match? ( @at , scheduled_at ) || check_for_inprecise_value ( scheduled_at )
168
+ end
169
+
170
+ def check_for_inprecise_value ( scheduled_at )
171
+ return unless Time === @at && values_match? ( @at . change ( usec : 0 ) , scheduled_at )
172
+
173
+ RSpec . warn_with ( ( <<-WARNING ) . gsub ( /^\s +\| / , '' ) . chomp )
174
+ |[WARNING] Your expected `at(...)` value does not match the job scheduled_at value
175
+ |unless microseconds are removed. This precision error often occurs when checking
176
+ |values against `Time.current` / `Time.now` which have usec precision, but Rails
177
+ |uses `n.seconds.from_now` internally which has a usec count of `0`.
178
+ |
179
+ |Use `change(usec: 0)` to correct these values.
180
+ |
181
+ |Note: RSpec cannot do this for you because jobs can be scheduled with usec
182
+ |precision and we do not know wether it is on purpose or not.
183
+ |
184
+ |
185
+ WARNING
186
+ false
167
187
end
168
188
169
189
def set_expected_number ( relativity , count )
Original file line number Diff line number Diff line change @@ -233,6 +233,17 @@ def self.name; "LoggingJob"; end
233
233
end
234
234
end
235
235
236
+ it "warns when time offsets are inprecise" do
237
+ expect ( RSpec ) . to receive ( :warn_with ) . with ( /precision error/ )
238
+
239
+ time = Time . current . change ( usec : 550 )
240
+ travel_to time do
241
+ expect {
242
+ expect { hello_job . set ( wait : 5 ) . perform_later } . to have_enqueued_job . at ( time + 5 )
243
+ } . to raise_error ( /expected to enqueue exactly 1 jobs/ )
244
+ end
245
+ end
246
+
236
247
it "accepts composable matchers as an at date" do
237
248
future = 1 . minute . from_now
238
249
slightly_earlier = 58 . seconds . from_now
You can’t perform that action at this time.
0 commit comments