Skip to content

Commit 3a36b1d

Browse files
Fix flaky organization_spec.rb and donnation_summary_spec.rb specs (#5521)
* reuse organization to create fewer factories * Use DateTime.current instead of DateTime.now Rails extends the Time and DateTime objects, and includes the `current`` property for retrieving the time the Rails environment is set to (default = UTC), as opposed to the server time (could be anything). https://stackoverflow.com/a/18811305 * Use same helper as the model for consistency * Time updates to fix flakiness Freezing the time and checking for the same time period should fix the flakiness. * Freeze entire block to prevent flakiness Time was being frozen after creating the factory. Move the test setup inside a freeze_time block to ensure created_at is created 1.day.ago. Plus, using the same date/time helper as the IssuedAt concern helps with keeping the test consistent with the code.
1 parent e182893 commit 3a36b1d

File tree

4 files changed

+26
-24
lines changed

4 files changed

+26
-24
lines changed

app/models/concerns/issued_at.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ def issued_at_cannot_be_before_2000
2121
end
2222

2323
def issued_at_cannot_be_further_than_1_year
24-
if issued_at.present? && issued_at > DateTime.now.next_year
24+
if issued_at.present? && issued_at > DateTime.current.next_year
2525
errors.add(:issued_at, "cannot be more than 1 year in the future")
2626
end
2727
end

spec/factories/donations.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
source { Donation::SOURCES[:misc] }
2323
storage_location
2424
organization { Organization.try(:first) || create(:organization) }
25-
issued_at { Time.current }
25+
issued_at { DateTime.current }
2626

2727
factory :manufacturer_donation do
2828
manufacturer

spec/models/organization_spec.rb

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,6 @@
107107

108108
describe 'users' do
109109
subject { organization.users }
110-
let(:organization) { create(:organization) }
111110

112111
context 'when a organizaton has a user that has two roles' do
113112
let(:user) { create(:user) }
@@ -361,7 +360,6 @@
361360
end
362361

363362
context 'with invisible items' do
364-
let!(:organization) { create(:organization) }
365363
let!(:item1) { create(:item, organization: organization, active: true, visible_to_partners: true) }
366364
let!(:item2) { create(:item, organization: organization, active: true, visible_to_partners: false) }
367365
let!(:item3) { create(:item, organization: organization, active: false, visible_to_partners: true) }
@@ -401,21 +399,24 @@
401399
describe 'earliest reporting year' do
402400
# re 2813 update annual report -- allowing an earliest reporting year will let us do system testing and staging for annual reports
403401
it 'is the organization created year if no associated data' do
404-
org = create(:organization)
405-
expect(org.earliest_reporting_year).to eq(org.created_at.year)
402+
expect(organization.earliest_reporting_year).to eq(organization.created_at.year)
406403
end
404+
407405
it 'is the year of the earliest of donation, purchase, or distribution if they are earlier ' do
408-
org = create(:organization)
409-
create(:donation, organization: org, issued_at: 364.days.from_now)
410-
create(:purchase, organization: org, issued_at: 364.days.from_now)
411-
create(:distribution, organization: org, issued_at: 364.days.from_now)
412-
expect(org.earliest_reporting_year).to eq(org.created_at.year)
413-
create(:donation, organization: org, issued_at: 5.years.ago)
414-
expect(org.earliest_reporting_year).to eq(5.years.ago.year)
415-
create(:purchase, organization: org, issued_at: 6.years.ago)
416-
expect(org.earliest_reporting_year).to eq(6.years.ago.year)
417-
create(:purchase, organization: org, issued_at: 7.years.ago)
418-
expect(org.earliest_reporting_year).to eq(7.years.ago.year)
406+
freeze_time do
407+
create(:donation, organization: organization, issued_at: DateTime.current.next_year - 1.day)
408+
create(:purchase, organization: organization, issued_at: DateTime.current.next_year - 1.day)
409+
create(:distribution, organization: organization, issued_at: DateTime.current.next_year - 1.day)
410+
expect(organization.earliest_reporting_year).to eq(organization.created_at.year)
411+
create(:donation, organization: organization, issued_at: 5.years.ago)
412+
expect(organization.earliest_reporting_year).to eq(5.years.ago.year)
413+
create(:purchase, organization: organization, issued_at: 6.years.ago)
414+
expect(organization.earliest_reporting_year).to eq(6.years.ago.year)
415+
create(:purchase, organization: organization, issued_at: 7.years.ago)
416+
expect(organization.earliest_reporting_year).to eq(7.years.ago.year)
417+
ensure
418+
travel_back
419+
end
419420
end
420421
end
421422

spec/requests/reports/donations_summary_spec.rb

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,16 @@
88
end
99

1010
describe "time display" do
11-
let!(:donation) { create(:donation, :with_items, issued_at: 1.day.ago) }
11+
it "uses issued_at for the relative time display, not created_at" do
12+
freeze_time do
13+
create(:donation, :with_items, issued_at: DateTime.current - 1.day)
1214

13-
before do
14-
freeze_time
15-
get reports_donations_summary_path
16-
end
15+
get reports_donations_summary_path
1716

18-
it "uses issued_at for the relative time display, not created_at" do
19-
expect(response.body).to include("1 day ago")
17+
expect(response.body).to include("1 day ago")
18+
ensure
19+
travel_back
20+
end
2021
end
2122
end
2223

0 commit comments

Comments
 (0)