Skip to content

A platform for community discussion. Free, open, simple.

DEV: Freeze time in gamification scorable specs (#43658)

Gabriel Grubba3b82052d10c8
diff --git a/plugins/discourse-gamification/spec/lib/scorables/shared_scorables_spec.rb b/plugins/discourse-gamification/spec/lib/scorables/shared_scorables_spec.rb
index 14ba4d73fec..59772ea1837 100755
--- a/plugins/discourse-gamification/spec/lib/scorables/shared_scorables_spec.rb
+++ b/plugins/discourse-gamification/spec/lib/scorables/shared_scorables_spec.rb
@@ -1,5 +1,9 @@
# frozen_string_literal: true
+# Scores are read through a materialized view that filters on PostgreSQL's
+# CURRENT_DATE, which is pinned to the start of the test transaction. Fixtures
+# created from the wall clock fall outside it when a run straddles UTC midnight,
+# so every shared example freezes the clock.
RSpec.shared_examples "Scorable Type" do
fab!(:leaderboard, :gamification_leaderboard)
let(:current_user) { Fabricate(:user) }
@@ -7,6 +11,8 @@ RSpec.shared_examples "Scorable Type" do
let(:third_user) { Fabricate(:user) }
let(:expected_score) { expected_score }
+ before { freeze_time DateTime.parse("2024-01-01 12:00") }
+
describe "#{described_class} updates gamification score" do
it "has correct total score" do
DiscourseGamification::GamificationLeaderboardScore.calculate_scores(
@@ -30,6 +36,8 @@ RSpec.shared_examples "Category Scoped Scorable Type" do
let(:expected_score) { described_class.score_multiplier }
let(:after_create_hook) { nil }
+ before { freeze_time DateTime.parse("2024-01-01 12:00") }
+
describe "updates gamification score" do
let!(:create_score) { class_action_fabricator }
let!(:trigger_after_create_hook) { after_create_hook }
@@ -70,6 +78,8 @@ RSpec.shared_examples "No Score Value" do
let(:class_action_fabricator_for_themselves) { nil }
let(:after_create_hook) { nil }
+ before { freeze_time DateTime.parse("2024-01-01 12:00") }
+
describe "#{described_class} awards no score value" do
let!(:create_score_for_deleted_object) { class_action_fabricator_for_deleted_object }
let!(:create_score_for_pm) { class_action_fabricator_for_pm }