Commit 9ecf5834 authored by David Fernandez's avatar David Fernandez

Merge branch 'add-rspec-tag-for-freezing-time' into 'master'

Add RSpec metadata tags for time traveling around specs

See merge request gitlab-org/gitlab!71346
parents 7d95f15f 032ebec0
......@@ -2,7 +2,7 @@
require 'spec_helper'
RSpec.describe Resolvers::DoraMetricsResolver do
RSpec.describe Resolvers::DoraMetricsResolver, time_travel_to: '2021-05-01' do
include GraphqlHelpers
let_it_be(:guest) { create(:user) }
......@@ -15,12 +15,6 @@ RSpec.describe Resolvers::DoraMetricsResolver do
let(:current_user) { reporter }
let(:args) { { metric: 'deployment_frequency' } }
around do |example|
travel_to '2021-05-01'.to_time do
example.run
end
end
before_all do
group.add_guest(guest)
group.add_reporter(reporter)
......
......@@ -3,9 +3,8 @@
require 'spec_helper'
RSpec.describe TrialStatusWidgetHelper, :saas do
describe 'data attributes for mounting Vue components' do
describe 'data attributes for mounting Vue components', :freeze_time do
let(:trial_length) { 30 } # days
let(:today_for_specs) { Date.parse('2021-01-15') }
let(:trial_days_remaining) { 18 }
let(:trial_end_date) { Date.current.advance(days: trial_days_remaining) }
let(:trial_start_date) { Date.current.advance(days: trial_days_remaining - trial_length) }
......@@ -23,7 +22,6 @@ RSpec.describe TrialStatusWidgetHelper, :saas do
end
before do
travel_to today_for_specs
build(:gitlab_subscription, :active_trial,
namespace: group,
trial_starts_on: trial_start_date,
......@@ -37,10 +35,6 @@ RSpec.describe TrialStatusWidgetHelper, :saas do
end
end
after do
travel_back
end
describe '#trial_status_popover_data_attrs' do
using RSpec::Parameterized::TableSyntax
......
......@@ -41,10 +41,8 @@ RSpec.describe Gitlab::Analytics::CycleAnalytics::RequestParams do
params[:created_before] = nil
end
it 'is valid' do
travel_to '2019-03-01' do
expect(subject).to be_valid
end
it 'is valid', time_travel_to: '2019-03-01' do
expect(subject).to be_valid
end
end
......
......@@ -9,11 +9,9 @@ RSpec.describe GitlabSubscription, :saas do
let_it_be(plan) { create(plan) }
end
describe 'default values' do
it do
travel_to(Date.today + 30) do
expect(subject.start_date).to eq(Date.today)
end
describe 'default values', time_travel: 30.days do
specify do
expect(subject.start_date).to eq(Date.today)
end
end
......
# frozen_string_literal: true
require 'active_support/testing/time_helpers'
RSpec.configure do |config|
config.include ActiveSupport::Testing::TimeHelpers
config.around(:example, :freeze_time) do |example|
freeze_time { example.run }
end
config.around(:example, :time_travel) do |example|
duration = example.metadata[:time_travel]
raise 'The time_travel RSpec metadata must have an ActiveSupport::Duration value (such as `30.days`).' unless duration.is_a?(ActiveSupport::Duration)
travel(duration) { example.run }
end
config.around(:example, :time_travel_to) do |example|
date_or_time = example.metadata[:time_travel_to]
unless date_or_time.respond_to?(:to_time) && date_or_time.to_time.present?
raise 'The time_travel_to RSpec metadata must have a Date or Time value.'
end
travel_to(date_or_time) { example.run }
end
end
# frozen_string_literal: true
require 'spec_helper'
RSpec.describe 'time travel' do
describe ':freeze_time' do
it 'freezes time around a spec example', :freeze_time do
expect { sleep 0.1 }.not_to change { Time.now.to_f }
end
end
describe ':time_travel' do
today = Date.current
it 'time-travels by the given duration', time_travel: 3.days do
expect(Date.current).to eq(today + 3.days)
end
it 'works with negative durations', time_travel: -5.days do
expect(Date.current).to eq(today - 5.days)
end
end
describe ':time_travel_to' do
it 'time-travels to the specified date', time_travel_to: '2020-01-01' do
expect(Date.current).to eq(Date.new(2020, 1, 1))
end
it 'time-travels to the specified date & time', time_travel_to: '2020-02-02 10:30:45 -0700' do
expect(Time.current).to eq(Time.new(2020, 2, 2, 17, 30, 45, '+00:00'))
end
end
end
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment