LoginSignup
2
1

More than 1 year has passed since last update.

【RSpec】実行日時によってテストの挙動が変わってしまうテストはtravel_toを使おう

Last updated at Posted at 2021-10-13

環境

Ruby 3.0.2
Rails 6.1.4.1

travel_toメソッド

travel_toブロックの中だけは指定した日時でテストを実行できる。

rails_helperの設定も忘れずに

rails_helper.rb
RSpec.configure do |config|
  config.include ActiveSupport::Testing::TimeHelpers
end
context 'travel_toメソッドテスト' do
  travel_to Time.zone.local(2021, 1, 1) do
    p Date.today #=> Fri, 01 Jan 2021
  end
end

参考

2
1
1

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
2
1