Sometimes, it can be tricky to write asserts for values that are nondeterministic, but flicker by a negligible amount, especially when working with time.
While I agree that the first line of defence should be to seek to freeze the time, sometimes it's not an option. Enter the "fuzzy" within
matcher.
# bad
is_expected.to(
be < execution_start.since(5.seconds)
.and(be >= execution_start)
)
# good
is_expected.to be_within(5.seconds).of(execution_start)
Docs on this matcher are rather limited and fail to mention that it can also be used in collections and arguments by omitting the be_
part.
is_expected.to(
change { record.field }.to(within(5.seconds).of(some_time))
)
expect(SomeClass).to(
have_received(:call)
.with(stamp: within(5.seconds).of(some_time))
.once
)