Skip to content

Instantly share code, notes, and snippets.

@qd3v
Created December 24, 2014 14:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save qd3v/f1becf9718cced46ad67 to your computer and use it in GitHub Desktop.
Save qd3v/f1becf9718cced46ad67 to your computer and use it in GitHub Desktop.
Timecop explained
t = Time.local(2008, 9, 1, 10, 5, 0)
# Self-explained
Timecop.freeze(t) do
expect(Time.zone.now.sec).to eq 0
end
# The same as above
Timecop.freeze(new_time)
expect(Time.zone.now.sec).to eq 0
Timecop.return
# Move to time and start counting as usual
Timecop.travel(t) do
sleep(1)
# travel time to + 1 sec
puts Time.now
end
# Combo. Travel to time, allow it run and treat one second as hour
Timecop.travel(t) do
Timecop.scale(3600) do
sleep(1)
# travel time + msecs + one hour
puts Time.now
sleep(1)
# travel time + msecs + two hours
puts Time.now
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment