class Timecop
def self.baseline
def self.baseline instance().send(:baseline) end
def self.baseline=(baseline)
def self.baseline=(baseline) instance().send(:baseline=, baseline) end
def self.freeze(*args, &block)
in your dev environment
which will lead to files being generated with the timestamp set by the Timecop.freeze call
rails project. Generators will load your environment, including the migration generator,
* Rails Users: Be especially careful when setting this in your development environment in a
forward.
benchmark or other timing calls are executed, which implicitly expect Time to actually move
* Note: Timecop.freeze will actually freeze time. This can cause unanticipated problems if
calls to Timecop.travel and have each block maintain it's concept of "now."
previous values after the block has finished executing. This allows us to nest multiple
When a block is also passed, Time.now, DateTime.now and Date.today are all reset to their
5. Timecop.freeze(year, month, day, hour=0, minute=0, second=0)
4. Timecop.freeze(offset_in_seconds)
3. Timecop.freeze(date_inst)
2. Timecop.freeze(datetime_inst)
1. Timecop.freeze(time_inst)
freeze and travel will respond to several different arguments:
end
assert joe.mortgage_due?
Timecop.freeze(2008, 10, 5) do
assert !joe.mortgage_due?
joe.purchase_home()
joe = User.find(1)
logic being tested. For example:
This is particularly useful for writing test methods where the passage of time is critical to the business
Allows you to run a block of code and "fake" a time throughout the execution of that block.
def self.freeze(*args, &block) instance().send(:travel, :freeze, *args, &block) Time.now end
def self.return
Reverts back to system's Time.now, Date.today and DateTime.now (if it exists).
def self.return instance().send(:unmock!) Time.now end
def self.return_to_baseline
def self.return_to_baseline instance().send(:return_to_baseline) Time.now end
def self.top_stack_item #:nodoc:
def self.top_stack_item #:nodoc: instance().instance_variable_get(:@_stack).last end
def self.travel(*args, &block)
good candidate for use in environment files in rails projects.
* Note: Timecop.travel will not freeze time (as opposed to Timecop.freeze). This is a particularly
See Timecop#freeze for a sample of how to use (same exact usage syntax)
Allows you to run a block of code and "fake" a time throughout the execution of that block.
def self.travel(*args, &block) instance().send(:travel, :travel, *args, &block) Time.now end
def baseline=(baseline)
def baseline=(baseline) @baseline = baseline @_stack << TimeStackItem.new(:travel, baseline) end
def initialize #:nodoc:
def initialize #:nodoc: @_stack = [] end
def return_to_baseline
def return_to_baseline if @baseline @_stack = [@_stack.shift] else unmock! end end
def travel(mock_type, *args, &block) #:nodoc:
def travel(mock_type, *args, &block) #:nodoc: stack_item = TimeStackItem.new(mock_type, *args) # store this time traveling on our stack... @_stack << stack_item if block_given? begin yield stack_item.time ensure # pull it off the stack... @_stack.pop end end end
def unmock! #:nodoc:
def unmock! #:nodoc: @baseline = nil @_stack = [] end