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

6.0.0 Release Loads .rspec Configuration #2623

Closed
gordysc opened this issue Oct 12, 2022 · 4 comments
Closed

6.0.0 Release Loads .rspec Configuration #2623

gordysc opened this issue Oct 12, 2022 · 4 comments

Comments

@gordysc
Copy link

gordysc commented Oct 12, 2022

What Ruby, Rails and RSpec versions are you using?

Ruby version: ruby 3.1.2p20 (2022-04-12 revision 4491bb740a) [arm64-darwin21]
Rails version: 7.0.4
RSpec 3.11

  • rspec-core 3.11.0
  • rspec-expectations 3.11.1
  • rspec-mocks 3.11.1
  • rspec-rails 6.0.0
  • rspec-support 3.11.1

Observed behaviour

For release 5.1.2 we could configure rspec-rails to use rails_helper.rb in the .rspec config file (instead of adding require rails_helper in each individual spec).

With release 6.0.0 we appear to now load the .rspec config file when we use the Rails generator for creating a model. This then loads rails_helper.rb which throws a pending migration error since the generated model's migration has not yet run.

For example:

rails g model user email password_digest

Failure/Error: abort e.to_s.strip

SystemExit:
  Migrations are pending. To resolve this issue, run:

          bin/rails db:migrate RAILS_ENV=development

  You have 1 pending migration:

Expected behaviour

We should do one of the following:

  1. Revert to the previous existing behavior (not sure if this change was intentional)
  2. Document that requiring rails_helper in the .rspec configuration is deprecated moving forward
  3. Or improve the check in the template rails_helper.rb when generating a model (unsure of how this would work)

Can you provide an example app?

https://github.com/gordysc/rspec-generator-issue

@pirj
Copy link
Member

pirj commented Oct 12, 2022

This is most probably caused by this change. We need to load .rspec, but certainly spec/rails_helper.rb is not required to run generators.

I'd suggest wrapping all the spec/rails_helper.rb in a if Rails.env.test? as a workaround.

@pirj pirj closed this as not planned Won't fix, can't repro, duplicate, stale Oct 12, 2022
@gordysc
Copy link
Author

gordysc commented Oct 13, 2022

Ah, that'd do it. Any reason that check couldn't be something added to the rails_helper that's generated on the install?

@pirj
Copy link
Member

pirj commented Oct 13, 2022

The primary reason is that some projects include parts that are testable without loading up the whole Rails env, and they don't need spec/rails_helper.rb. Some specs, e.g. custom cops, would require a different helper, spec/rubocop_helper.rb etc.

Our RSpec Core's .rspec initializer only adds --require spec_helper.

There are also some scenarios when specs are called initially in the development env, and this is why we force-set the env to be test here.

@JonRowe
Copy link
Member

JonRowe commented Oct 14, 2022

We could add a note about it to the generated rails_helper.rb as this may be hit more than we think, I'd happily review a PR for that @gordysc

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants