I've noticed that projects such as bundler do a require spec_helper
in each spec file
I've also noticed that rspec takes the option --require
, which allows you to require a file when rspec is bootstrapped. You can also add this to the .rspec
file, so it is added whenever you run rspec with no arguments.
Are there any disadvantages to using the above method which might explain why projects such as bundler choose to require spec_helper in each spec file?
I don't work on Bundler so I can't speak directly about their practices. Not all projects check-in the .rspec
file. The reason is this file, generally by current convention, only has personal configuration options for general output / runner preferences. So if you only required spec_helper
there, others wouldn't load it, causing tests to fail.
Another reason, is not all tests may need the setup performed by spec_helper
. More recently there have been groups of Rubyists who are trying to move away from loading too many dependencies into the test. By making it explicit when spec_helper
is required in the test people have an idea what may be going on. Also, running a single test file or directory that doesn't need that setup will be faster.
In reality, if all of your tests are requiring spec_helper
and you've make it a clear convention on the project there's no technical reason you can't or shouldn't do it. It just may be an initial surprise for new people who join the project.
With a proper setup, there's no downside at all.
The .rspec
file is meant to be project related (and should be commited like any other project source file).
Meanwhile, the .rspec-local
is for overriding with personalized settings (and it will let the user override some options only).
(see: https://www.relishapp.com/rspec/rspec-core/v/3-2/docs/configuration/read-command-line-configuration-options-from-files)
Rails projects even use a separate --require rails_helper
for Rails-specific RSpec settings.
Both the .rspec
and --require
options have existed since 2011 at least (which is ages ago).
And, RSpec is especially not a tool for people needing training wheels - you want people to understand how RSpec works and what the options are, because you want people to know e.g. when and where to temporarily set the --seed
option, how to change the formatter, switch on --fail-fast
, use specific tags to work on a feature, etc.
The test environment also has to have a consistent configuration, so you do not want people tweaking the spec_helper
file or the .rspec
file (for the same reason).
In short: if it doesn't apply to every spec in the project, it shouldn't be in the spec_helper
file. Which is why you should make sure it is included by every spec file. And the .rspec
file is the best way to do that.
The only reason to not to switch to this is when the project is being actively maintained by many people (and any project wide change just creates annoyances by e.g. forcing people to rebase their work for no reason related to what they were working on).
Bundler fits into this category - to many people potentially working concurrently.
P.S. I also recommend using rspec --init
on an empty project and checking out the generated config.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With