A timecop inspire library allow "time travel", "freezing time" and "time acceleration" capabilities, making it simple to test time-dependent code.
Add this to your application's shard.yml:
dependencies:
timecop:
github: TobiasGSmollett/timecop.crrequire"timecop"date =Time.new(2008, 10, 10, 10, 10, 10)
Timecop.freeze(date) do |frozen_time|
puts"#{frozen_time ==Time.now}"# => trueendTimecop.travel(Time.new(2014, 1, 1, 0, 0, 0)) doputsTime.now # => 2014-01-01 00:00:00 +0900
sleep(5)
putsTime.now # => 2014-01-01 00:00:05 +0900end# seconds will now seem like hoursTimecop.scale(Time.now, 3600)
putsTime.now # => 2017-08-28 23:50:06 +0900
sleep(2.0)
# seconds later, hours have passed and it's gone from 23pm at night to 1am in the morningputsTime.now # => 2017-08-29 01:50:21 +0900# turn on safe modeTimecop.safe_mode =trueTimecop.safe_mode? # => true# using method without blockTimecop.freeze Time.new(2008, 10, 10, 10, 10, 10)
# => Timecop::SafeModeException: Safe mode is enabled, only calls passing a block are allowed.Pull Request Welcome
- Fork it ( https://github.com/TobiasGSmollett/timecop.cr/fork )
- Create your feature branch (git checkout -b my-new-feature)
- Commit your changes (git commit -am 'Add some feature')
- Push to the branch (git push origin my-new-feature)
- Create a new Pull Request
- TobiasGSmollett TobiasGSmollett - creator, maintainer
Thanks to Travis Jeffery for his awesome work on timecop.