LoginSignup
4
0

More than 1 year has passed since last update.

RSpecでtravel_toを使ってみた

Last updated at Posted at 2022-12-18

はじめに

ポートフォリオの作成で初めてtravel_toを使ったので記事にしました。

タイムゾーンを変更する

時間の変化を分かりやすくするため、タイムゾーンを日本に設定します。

config/application.rb
class Application < Rails::Application
  config.time_zone = 'Tokyo'
end

travel_to

putsで時間を表示させると、タイムスタンプが変わっていることが分かります。

spec/system/posts_spec.rb
  describe 'travel_toを使ってみた' do
    it '24時間前、現在、24時間後' do
      travel_to Time.zone.now.yesterday do
        puts Time.zone.now
      end
      puts Time.zone.now
      travel_to Time.zone.now.tomorrow do
        puts Time.zone.now
      end
    end
  end
結果
Posts
  travel_toを使ってみた
2022-12-17 18:02:46 +0900
2022-12-18 18:02:46 +0900
2022-12-19 18:02:46 +0900
    24時間前、現在、24時間後

Finished in 0.04604 seconds (files took 3.92 seconds to load)
1 example, 0 failures

参考
https://qiita.com/mightysosuke/items/72f764fc237422b423b0

4
0
0

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
4
0