LoginSignup
25

More than 5 years have passed since last update.

Delorean.back_to_the Time.zone.local(2012, 7, 18) do

テストを書いている時に、境界値分析で特定の時刻をテストしたいということがよくあります。日をまたぐ場合とか、経過時間であれこれするとか、ゲームなんかだとよくありますね。
そういう時にテストケースを書く助けになるのがDeloreanです。

Gemfile
group :development, :test do
  gem 'delorean'
end

特定の時刻にジャンプ

back_to_the_future.rb
Delorean.time_travel_to Time.zone.local(2015, 10, 21, 4, 30, 45)
p Time.zone.now #=> Wed, 21 Oct 2015 04:30:45 JST +09:00

一日前

yesterday.rb
Delorean.time_travel_to 1.day.ago

現在に戻る

today.rb
Delorean.back_to_the_present

ブロック

block.rb
Delorean.time_travel_to 3.year.ago do
  p Time.zone.now
end

なお、ブロックの中でも時間は経過するので、

block.rb
Delorean.time_travel_to Time.zone.local(2015, 10, 21, 4, 30, 45)
  sleep 30
  p Time.zone.now #=> Wed, 21 Oct 2015 04:31:15 JST +09:00
end

のようになります。ぴったりある時刻になるようなテストケースを書くには工夫が必要です。

なお、back_to_the_presentback_to_the_1985とも書けます。1985年には戻りません。

end

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
25