Module: Postburner::TimeHelpers
- Included in:
- NullQueue, TimeTravelTestQueue
- Defined in:
- lib/postburner/time_helpers.rb
Overview
Requires ActiveSupport::Testing::TimeHelpers (Rails testing framework)
Time travel affects global time via Time.stub - use only in test environments
Provides time travel utilities for testing without requiring test context.
This module wraps ActiveSupport::Testing::TimeHelpers to enable time travel from non-test contexts (like class methods or service objects). It creates a helper object extended with Rails’ TimeHelpers and delegates time travel operations to it.
## Usage
Instance Method Summary collapse
-
#travel_to(time, &block) ⇒ Object
Travels to specified time for block execution using Rails time helpers.
Instance Method Details
#travel_to(time, &block) ⇒ Object
Travels to specified time for block execution using Rails time helpers.
Creates a helper object extended with ActiveSupport::Testing::TimeHelpers and uses it to stub global time. This is necessary for time travel outside of test method contexts where TimeHelpers aren’t available directly.
The travel_to call stubs Time globally (using Time.stub), so all time-based operations within the block execute as if they’re happening at the specified time. After the block completes, time returns to normal.
67 68 69 70 71 72 73 74 75 76 77 |
# File 'lib/postburner/time_helpers.rb', line 67 def travel_to(time, &block) unless defined?(ActiveSupport::Testing::TimeHelpers) raise <<~ERROR ActiveSupport::Testing::TimeHelpers not available. Postburner::TimeHelpers requires Rails testing helpers for time travel. ERROR end helper = Object.new.extend(ActiveSupport::Testing::TimeHelpers) helper.travel_to(time, &block) end |