Mock with Time.now
with the power of time travel, time freeze and time scale.
Inspired by timecop ruby gem
Add this to your application's shard.yml
:
development_dependencies:
timecop:
github: waterlink/timecop.cr
Timecop.freeze
(normal and block forms)Timecop.reset
-
Timecop.freeze
-
Timecop.reset
-
Timecop.travel
-
Timecop.scale
- Support nested block forms of scale, freeze and travel
require "timecop"
NOTE: Not implemented yet
Timecop.travel(2.days.ago)
# Do some stuff 2 days ago
Timecop.travel(4.days.from_now)
# Do some stuff 2 days from now (2 days ago + 4 days from now = -2 + 4 = +2)
Block form:
# Normal time here
Timecop.travel(2.days.ago) do
# Do some stuff 2 days ago
end
# Again back to normal time
Timecop.freeze(2.days.ago)
# Do some stuff exactly 2 days ago
Block form:
Timecop.freeze(2.days.ago) do
# Time.now is always 2 days ago in this block
end
Timecop.freeze
is used to statically mock the concept of now. As your program executes, Time.now
will always return the same value - value you provided as an argument to Timecop.freeze
.
On the other hand Timecop.travel
is dynamically mocks Time.now
, ie: as your program executes, Time.now
will change accordingly taking into account offset created by Timecop.travel
.
Timecop.travel
and Timecop.freeze
both support nested calls. NOTE: Not implemented yet
NOTE: Not implemented yet
Timecop.scale(1000)
# Now time goes 1000 times faster
Timecop.scale(0.001)
# Now time goes as normal
Timecop.scale(0.001)
# Now time goes 1000 times slower
Block form:
Timecop.scale(2.5) do
# here time goes 2.5 times faster
end
Timecop.reset
To run specs run bin/spec
- Fork it ( https://github.com/waterlink/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