diff options
author | GitLab Bot <gitlab-bot@gitlab.com> | 2021-11-21 18:12:28 +0300 |
---|---|---|
committer | GitLab Bot <gitlab-bot@gitlab.com> | 2021-11-21 18:12:28 +0300 |
commit | f02b3776bf8981482c558753c40120e0d644f13a (patch) | |
tree | beda3eb8c43beeaa2dac0a53f1344e32dbe26224 /doc/development/testing_guide | |
parent | 9d22a691022bccd485e0f1b01be3d9dd5378dd18 (diff) |
Add latest changes from gitlab-org/gitlab@master
Diffstat (limited to 'doc/development/testing_guide')
-rw-r--r-- | doc/development/testing_guide/best_practices.md | 36 |
1 files changed, 36 insertions, 0 deletions
diff --git a/doc/development/testing_guide/best_practices.md b/doc/development/testing_guide/best_practices.md index 152edee9921..fab75581855 100644 --- a/doc/development/testing_guide/best_practices.md +++ b/doc/development/testing_guide/best_practices.md @@ -739,6 +739,42 @@ it 'is overdue' do end ``` +#### RSpec helpers + +You can use the `:freeze_time` and `:time_travel_to` RSpec metadata tag helpers to help reduce the amount of +boilerplate code needed to wrap entire specs with the [`ActiveSupport::Testing::TimeHelpers`](https://api.rubyonrails.org/v6.0.3.1/classes/ActiveSupport/Testing/TimeHelpers.html) +methods. + +```ruby +describe 'specs which require time to be frozen', :freeze_time do + it 'freezes time' do + right_now = Time.now + + expect(Time.now).to eq(right_now) + end +end + +describe 'specs which require time to be frozen to a specific date and/or time', time_travel_to: '2020-02-02 10:30:45 -0700' do + it 'freezes time to the specified date and time' do + expect(Time.now).to eq(Time.new(2020, 2, 2, 17, 30, 45, '+00:00')) + end +end +``` + +[Under the hood](https://gitlab.com/gitlab-org/gitlab/-/blob/master/spec/support/time_travel.rb), these helpers use the `around(:each)` hook and the block syntax of the +[`ActiveSupport::Testing::TimeHelpers`](https://api.rubyonrails.org/v6.0.3.1/classes/ActiveSupport/Testing/TimeHelpers.html) +methods: + +```ruby +around(:each) do |example| + freeze_time { example.run } +end + +around(:each) do |example| + travel_to(date_or_time) { example.run } +end +``` + ### Feature flags in tests This section was moved to [developing with feature flags](../feature_flags/index.md). |