Can we go anywhere we want at any time?
You can do anything you want.
Check out the new version about time travel with RSpec!
![](http://3.bp.blogspot.com/_-dK4R3d1lbc/SDu6mEjFkgI/AAAAAAAAAec/xUgS4JFUd-k/s320/timetunnel.jpg)
Just testing this piece of code if the day the tests are being run happens to be below the 11th day of the month is not a solution, of course. So I googled a bit and found this wonderful piece of code.
These are the steps to get your Rails Time Machine (tm) up and running:
- Copy the above code to a file and save it as test/time_helper.rb
- Require it in your test:
require File.dirname(__FILE__) + '/../time_helper'
- Either set the time for all the tests in the file:
def setup
pretend_now_is(Time.local(2007, 8, 1)) # position *all* tests back in time!
end
def teardown
Time.reset # jump back to the present
end
or just do some time travel for a code block:
(These code samples come from here. Thank you!)
def test_decades
pretend_now_is(Time.local(1960)) do
assert_equal(1960, Time.now.year)
end
pretend_now_is(Time.local(1970)) do
assert_equal(1970, Time.now.year)
end
# ...
end - Find a street where your De Lorean can reach 88 mph and run your time traveling tests!
If this was useful for you, please take a minute and recommend me:
![Recommend Me](http://workingwithrails.com/images/tools/compact-small-button.jpg)
Thank you!