Last active
June 15, 2022 14:01
-
-
Save cutalion/10231921 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'active_support/testing/time_helpers' | |
RSpec.configure do |config| | |
config.include ActiveSupport::Testing::TimeHelpers | |
config.after do | |
travel_back | |
Timecop.return | |
end | |
config.extend Module.new { | |
def freeze_time_around(time = Time.current) | |
around do |ex| | |
time = Time.zone.parse(time) if time.is_a? String | |
Timecop.freeze(time) do | |
ex.run | |
end | |
end | |
end | |
} | |
config.around(:each, :freeze_time) do |ex| | |
time = ex.metadata[:freeze_time] | |
time = case time | |
when String | |
Time.zone.parse(time) | |
when nil, true | |
Time.zone.now | |
else | |
time | |
end | |
if time | |
Timecop.freeze(time) do | |
ex.run | |
end | |
else | |
ex.run | |
end | |
end | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
describe "Start" do | |
it "should save start time", :freeze_time do | |
subject.start; | |
expect(subject.started_at).to eq Time.zone.now | |
end | |
context 'stop war', freeze_time: '2022-02-24' do | |
it 'works' do | |
expect(Date.today.to_s).to eq '2022-02-24' | |
end | |
end | |
context 'with time object', freeze_time: 2.hours.since do | |
it 'works' do | |
# ... | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment