Skip to content

Commit

Permalink
Merge pull request #1939 from gnclmorais/introduce-timecop
Browse files Browse the repository at this point in the history
Introduce Timecop for more stable time testing
  • Loading branch information
matyikriszta committed Sep 29, 2023
2 parents 068e7d6 + 66266d7 commit af87097
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 6 deletions.
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ group :test do
gem 'shoulda-matchers', '~> 4.5'
gem 'simplecov', require: false
gem 'simplecov-lcov', require: false
gem 'timecop', '~> 0.9.8'
end

group :production do
Expand Down
2 changes: 2 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -451,6 +451,7 @@ GEM
execjs (>= 0.3.0, < 3)
thor (1.2.2)
tilt (2.2.0)
timecop (0.9.8)
timeout (0.4.0)
turbo-rails (1.4.0)
actionpack (>= 6.0.0)
Expand Down Expand Up @@ -553,6 +554,7 @@ DEPENDENCIES
stimulus-rails
stripe
terser
timecop (~> 0.9.8)
turbo-rails
tzinfo-data
web-console (>= 4.1.0)
Expand Down
4 changes: 4 additions & 0 deletions config/environments/test.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
require "active_support/core_ext/integer/time"
require "timecop"

# The test environment is used exclusively to run your application's
# test suite. You never need to work with it otherwise. Remember that
Expand Down Expand Up @@ -74,4 +75,7 @@
Bullet.bullet_logger = true
Bullet.raise = false # raise an error if n+1 query occurs
end

# https://github.com/travisjeffery/timecop#timecopsafe_mode
Timecop.safe_mode = true
end
14 changes: 8 additions & 6 deletions spec/models/concerns/listable_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,16 @@
context 'scopes' do
context '#today_and_upcoming' do
it 'returns a list of all today and upcoming workshops' do
Fabricate.times(5, :past_workshop)
future_workshops = Fabricate.times(3, :workshop)
Timecop.travel(Time.now.utc) do
Fabricate.times(5, :past_workshop)
future_workshops = Fabricate.times(3, :workshop)

# Make sure one is not technically upcoming but is happening now
future_workshops.last.date_and_time = 1.hour.ago
future_workshops.last.save
# Make sure one is not technically upcoming but is happening now
future_workshops.last.date_and_time = 1.hour.ago
future_workshops.last.save

expect(Workshop.today_and_upcoming).to match_array(future_workshops)
expect(Workshop.today_and_upcoming).to match_array(future_workshops)
end
end
end

Expand Down

0 comments on commit af87097

Please sign in to comment.