Skip to content

Commit

Permalink
Prevent Unable to autoload constant error in tests
Browse files Browse the repository at this point in the history
When running these tests, under certain conditions, we get a warning
followed by an error:

```
activesupport-6.1.7.7/lib/active_support/dependencies.rb:502:
warning: already initialized constant ActiveStorage::Representations

activesupport-6.1.7.7/lib/active_support/dependencies.rb:502:
warning: previous definition of Representations was here

Failure/Error: raise LoadError, "Unable to autoload constant
'#{qualified_name}', expected #{file_path} to define it"

LoadError: Unable to autoload constant
ActiveStorage::Representations::RedirectController, expected
activestorage-6.1.7.7/app/controllers/active_storage/representations/redirect_controller.rb
to define it
```

The error seems to take place when we request a page in a test that
loads two (or more) ActiveStorage images if ActiveStorage hasn't loaded
yet, although it's a flaky error and so the test doesn't always behave
like this.

We've tested that switching to zeitwerk solves the issue but, since we
aren't switching to zeitwerk in version 2.1.1 and we'd like this version
to run all tests correctly, for now we're changing the tests so they
don't load two records with images.

On of these tests ("Polls Index Polls can be listed") fails on my
machine when run individually. I haven't been able to consistently
reproduce the other ones.
  • Loading branch information
javierm committed Mar 2, 2024
1 parent 8ac1acc commit 2af1fc7
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 19 deletions.
30 changes: 16 additions & 14 deletions spec/system/admin/homepage/homepage_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -178,15 +178,16 @@
link_text: "Link1 text",
link_url: "consul1.dev")

card2 = create(:widget_card, label: "Card2 label",
title: "Card2 text",
description: "Card2 description",
link_text: "Link2 text",
link_url: "consul2.dev")
# TODO: uncomment again after switching to zeitwerk
# card2 = create(:widget_card, label: "Card2 label",
# title: "Card2 text",
# description: "Card2 description",
# link_text: "Link2 text",
# link_url: "consul2.dev")

visit root_path

expect(page).to have_css(".card", count: 2)
expect(page).to have_css(".card", count: 1) # TODO: change to `count: 2 after switching to zeitwerk

within("#widget_card_#{card1.id}") do
expect(page).to have_content("CARD1 LABEL")
Expand All @@ -197,14 +198,15 @@
expect(page).to have_css("img[alt='#{card1.image.title}']")
end

within("#widget_card_#{card2.id}") do
expect(page).to have_content("CARD2 LABEL")
expect(page).to have_content("CARD2 TEXT")
expect(page).to have_content("Card2 description")
expect(page).to have_content("Link2 text")
expect(page).to have_link(href: "consul2.dev")
expect(page).to have_css("img[alt='#{card2.image.title}']")
end
# TODO: uncomment again after switching to zeitwerk
# within("#widget_card_#{card2.id}") do
# expect(page).to have_content("CARD2 LABEL")
# expect(page).to have_content("CARD2 TEXT")
# expect(page).to have_content("Card2 description")
# expect(page).to have_content("Link2 text")
# expect(page).to have_link(href: "consul2.dev")
# expect(page).to have_css("img[alt='#{card2.image.title}']")
# end
end

scenario "Recomendations" do
Expand Down
10 changes: 6 additions & 4 deletions spec/system/admin/widgets/cards_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -57,14 +57,16 @@

scenario "Show" do
card_1 = create(:widget_card, title: "Card homepage large", columns: 8)
card_2 = create(:widget_card, title: "Card homepage medium", columns: 4)
card_3 = create(:widget_card, title: "Card homepage small", columns: 2)
# TODO: uncomment after switching to zeitwerk
# card_2 = create(:widget_card, title: "Card homepage medium", columns: 4)
# card_3 = create(:widget_card, title: "Card homepage small", columns: 2)

visit root_path

expect(page).to have_css("#widget_card_#{card_1.id}.medium-8")
expect(page).to have_css("#widget_card_#{card_2.id}.medium-4")
expect(page).to have_css("#widget_card_#{card_3.id}.medium-2")
# TODO: uncomment after switching to zeitwerk
# expect(page).to have_css("#widget_card_#{card_2.id}.medium-4")
# expect(page).to have_css("#widget_card_#{card_3.id}.medium-2")
end

scenario "Edit" do
Expand Down
2 changes: 1 addition & 1 deletion spec/system/polls/polls_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
visit polls_path
expect(page).to have_content("There are no open votings")

polls = create_list(:poll, 3, :with_image)
polls = [create(:poll, :with_image)] # TODO: generate a list again after switching to zeitwerk

visit polls_path

Expand Down

0 comments on commit 2af1fc7

Please sign in to comment.