Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prevent "Unable to autoload constant" error in tests #5422

Merged
merged 4 commits into from
Mar 18, 2024
Merged

Conversation

javierm
Copy link
Member

@javierm javierm commented Mar 2, 2024

References

Background

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

Objectives

  • Make our test suite more reliable

Notes

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.

@javierm javierm self-assigned this Mar 2, 2024
@javierm javierm added this to Reviewing in Consul Democracy Mar 2, 2024
@javierm javierm force-pushed the fix_polls_test branch 2 times, most recently from 350896c to dda0761 Compare March 2, 2024 17:44
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.
Creating records after starting the browser with the `visit` method
sometimes results in database corruption and failing tests on our CI.

Splitting some tests or merging them together solves the issue.
This way we avoid variables starting with underscores, which we don't
use almost anywhere else.
@taitus taitus added the 2.1.1 label Mar 8, 2024
@taitus taitus self-assigned this Mar 18, 2024
@javierm javierm changed the title Prevent Unable to autoload constant error in tests Prevent "Unable to autoload constant" error in tests Mar 18, 2024
Consul Democracy automation moved this from Reviewing to Testing Mar 18, 2024
This test is failing often due to an "Unable to autoload constant"
error, that will be fixed after switching to zeitwerk.

Just like it happened in the the "Polls can be listed" test, the reason
seems to be accessing a page containing several ActiveStorage
attachments. However, since this test only makes sense when two or more
images are displayed on the page, we can't change the test so create
just one image.

So, for now, we're commenting the test, and we'll uncomment it again
when we enable zeitwerk in version 2.2.0.
@javierm javierm merged commit 0972bcb into master Mar 18, 2024
13 checks passed
Consul Democracy automation moved this from Testing to Release 2.1.1 Mar 18, 2024
@javierm javierm deleted the fix_polls_test branch March 18, 2024 14:28
@javierm javierm removed the 2.1.1 label Mar 18, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants