Time Mocking¶
BDD::Behave ships with Timecop-style helpers for freezing and traveling
through time inside an example. They work by wrapping Raku's now,
Instant.now, DateTime.now, and Date.today so the wrap consults a
dynamically-scoped freeze. When no freeze is active, the wrappers fall
through to the real time source — they have no effect outside a freeze block.
freeze-time¶
1 2 3 4 5 6 7 8 | |
Freeze at an explicit moment with a DateTime, Instant, or ISO 8601 string:
1 2 3 4 5 6 7 | |
Date.today is also frozen to the same instant:
1 2 3 4 5 | |
travel-to¶
travel-to is a synonym for the explicit-moment form of freeze-time:
1 2 3 | |
travel-by¶
Inside an active freeze block, travel-by advances the frozen instant
forward by a Duration (or a plain Real number of seconds):
1 2 3 4 5 6 7 | |
Multiple advances compose:
1 2 3 4 5 6 | |
Calling travel-by outside an active freeze dies with a clear message.
Nested freezes¶
Inner freezes shadow outer ones. When the inner block exits, the outer freeze is restored:
1 2 3 4 5 6 7 8 9 | |
Restoration¶
Time is always restored when a freeze block exits, including when the block throws. The frozen state never leaks to the next example.
:freeze-time metadata¶
Examples and groups can opt into a freeze via the :freeze-time metadata.
The runner wraps the example body in a freeze around before-each /
after-each hooks — wall-clock duration is measured outside the freeze, so
the example's started-at / finished-at / duration accessors still
report real time.
Freeze at the moment the example body begins:
1 2 3 4 5 6 | |
Freeze at an explicit moment:
1 2 3 | |
Pass :freeze-time(False) to explicitly opt out (useful when overriding an
inherited group-level freeze).
current-time¶
current-time returns the current Instant — frozen when a freeze is
active, real now otherwise. Use it from helpers that want to read "time"
without bypassing the freeze.
1 2 3 | |