やったこと
Rails5 で時間に関わるテストを rspec で書いてみます。
確認環境
$ ruby --version
ruby 2.6.3p62 (2019-04-16 revision 67580) [x86_64-darwin17]
$ rails --version
Rails 5.2.3
$ gem list | grep rspec-rails
rspec-rails (3.8.2)
調査
$ rails g rspec:model Task
spec/rails_helper.rb
RSpec.configure do |config|
...
config.include ActiveSupport::Testing::TimeHelpers
...
end
spec/models/task_spec.rb
require 'rails_helper'
RSpec.describe Task, type: :model do
it 'is whether now time' do
travel_to('2019-05-26 23:00'.to_time) do
expect(Time.zone.now).to eq(Time.new(2019, 5, 26, 23, 0, 0))
end
end
end