-
Notifications
You must be signed in to change notification settings - Fork 229
Automatically require rails_helper in all specs #36
Comments
Thanks, clever idea. Maybe I should collect ideas like this in an appendix at some point. |
It's fine to choose to do this. Just be aware that there is a reason why the default With RSpec 3, the general advise is that the The benefit here is that I only need to load Rails if / when I need it. For example, say I have an external service wrapper in my app. This object is not coupled to Rails in any way. When I build out this object I can run its spec in isolation without constantly paying the Rails startup tax. This same logic can be applied to any other heavy dependency your app may need. |
Good point @cupakromer. As a project gets more complex, I'll typically extract these out into separate gems with their own specs, leaving very few specs (if any) where I don't need Rails. An alternative strategy I might use in your case, assuming you're using the guard :rspec, cmd: 'spring rspec --require rails_helper' do
# spec paths that require 'rails_helper.rb'
end
guard :rspec, cmd: 'spring rspec --require extensions_helper' do
# spec paths that require 'extensions_helper.rb'
end
guard :rspec, cmd: 'spring rspec' do
# spec paths with no requirements other than what's in spec_helper.rb
end This way way, I'm much leaner when running individual specs and when I want to run the entire suite, I can run rspec with something like this: bundle exec rspec --require meta_helper In this case, meta_helper would be a file that requires all the other helpers, to ensure all dependencies are met for every spec. Thoughts? Is there a simpler solution that isn't coming to my mind right now? I really like to avoid all those |
Yep, @chrisvfritz that's a completely valid point ❤️. I agree with you that in those cases it's a good time to consider your suggestion. To be clear, I wasn't trying to say to never do what you suggested. Just that people should understand the trade offs of the decision. Personally in my recent projects, I can (and eventually do) move any code in
I don't use guard, zeus, or any of the other preloads. The one exception I make currently is spring; but only because it is now bundled with Rails. I typically use editor plugins to run a single spec file, a subset of specs, a single spec, or the entire suite via hotkeys/shortcuts. Something else to be aware of with this type of setup is that spec types are bound to directory paths. It's true that rspec-rails 2.x forced this. rspec-rails 3.x is moving away from this, allowing users to be more flexible in where they put different spec types. While I still generally follow the same layout of where specs live based on general "type", it's not always true they have the same core dependencies; such as in the case of the domain models (all Please understand I'm not saying I disagree with the use of guard, zeus, etc. My personal style just does not mesh well with how they work. I often save my files frequently, leaving invalid Ruby syntax, incomplete thoughts, etc. This causes frequent spec runs which fail, as I expect, but it generates too much noise for my personal taste. Sadly 😿, I've also had multiple bad experiences with those gems where specs get run multiple times or are never run. This causes me to spend more time debugging the setup then actually working on the code. Again, just my personal experience. If things work for you 👍 keep using them! 😸
I can't think of a simpler solution. I have no problems with multiple Seeing the I should note that my There are a few cases where do require a spec support file that has multiple sub-responsibilities. For example a general "top level" custom matcher file. However, in those instances I try to keep all dependent requires in one file and use These days Ruby is good at handling seeing the same |
All great points! Great conversation. 😄 |
Yes, great convo! 😄 I should note that I do use |
I actually like to add
--require rails_helper
to a.rspec
file in my project root. This has the effect that all my specs are automatically run with rails_helper pre-required. You can do this with other parameters you'd like to use when running rspec as well. My full.rspec
file reads:The text was updated successfully, but these errors were encountered: