WHAT THIS FORK DOES
This is a fork of the Timecop gem. This fork make use of file caches to store the offset and values of the mock time objects. This allows different processes to read and write to the mock objects. The motivation is to enable Culerity to use timecop.
Two timecop cache files are created in your home directory. They are .timecop-cache.time.cache and .timecop-cache.offset.cache
#Cucumber feature Scenario: Invitees receives reminder email when event approaches Given event "Weather Conference" is published And the event starts at "2006-March-3 00:00" And the event has set reminder emails to be sent "2" days before the event starts And the event has the following invitees: """ email@example.com, firstname.lastname@example.org, email@example.com """ And the time now is "1-3-2006 00:00" When the scheduler sends the reminder emails Then "firstname.lastname@example.org, email@example.com, firstname.lastname@example.org" should receive a "Reminder to attend Weather Conference" email #In your step definitions Given /^the time now is "(\d+)-(\d+)-(\d+) (\d\d):(\d\d)"$/ do |day, month, year, hr, min| Timecop.freeze(Time.local(year, month, day, hr, min)) end #In your env.rb Before do clear_time_cache end def clear_time_cache time_cache = File.expand_path('~/.timecop-cache.time.cache') offset_cache = File.expand_path('~/.timecop-cache.offset.cache') File.delete(time_cache) if File.exist?(time_cache) File.delete(offset_cache) if File.exist?(offset_cache) end
A gem providing “time travel” and “time freezing” capabilities, making it dead simple to test time-dependent code. It provides a unified method to mock Time.now, Date.today, and DateTime.now in a single call.
Freeze time to a specific point.
Travel back to a specific point in time, but allow time to continue moving forward from there.
Timecop api allows arguments to be passed into #freeze and #travel as one of the following:
individual arguments (year, month, day, hour, minute, second)
a single integer argument that is interpreted as an offset in seconds from Time.now
Nested calls to Timecop#travel and Timecop#freeze are supported – each block will maintain its interpretation of now.
Run a time-sensitive test
joe = User.find(1) joe.purchase_home() assert !joe.mortgage_due? # move ahead a month and assert that the mortgage is due Timecop.freeze(Date.today + 30) do assert joe.mortgage_due? end
Set the time for the test environment of a rails app – this is particularly helpful if your whole application is time-sensitive. It allows you to build your test data at a single point in time, and to move in/out of that time as appropriate (within your tests)
config.after_initialize do # Set Time.now to September 1, 2008 10:05:00 AM (at this instant), but allow it to move forward t = Time.local(2008, 9, 1, 10, 5, 0) Timecop.travel(t) end
The difference between Timecop.freeze and Timecop.travel
#freeze is used to statically mock the concept of now. As your program executes, Time.now will not change unless you make subsequent calls into the Timecop API. #travel, on the other hand, computes an offset between what we currently think Time.now is (recall that we support nested traveling) and the time passed in. It uses this offset to simulate the passage of time. To demonstrate, consider the following code snippets:
new_time = Time.local(2008, 9, 1, 12, 0, 0) Timecop.freeze(new_time) sleep(10) new_time == Time.now # ==> true Timecop.return # "turn off" Timecop Timecop.travel(new_time) sleep(10) new_time == Time.now # ==> false
sudo gem install liangzan-timecop