← Back to Upcase

Rspec 'support' vs 'helpers'?

(Michael Merrill) #1

Hi All,

In the Test-Driven Rails Tutorial we put methods we need access to across all specs in a folder we label as ‘support’. I noticed a few users were having issues accessing that folder until they required it manually in rails_helper. I also noticed running rspec:install gave us a ‘helper’ folder. Do new releases of Rspec use the ‘helper’ folder instead of ‘support’ and are they considered same? If so, is there any benefit to one over the other?

(Robert Eshleman) #2

The spec/support/ directory, as you mention, is conventionally where support files (custom matchers, shared examples, helper methods for specs, etc.) for the tests themselves go.

You’re also correct in noting that files in this directory need to be required by rails_helper.rb. Often you’ll see something like this:

Dir[Rails.root.join("spec/support/**/*.rb")].each { |file| require file }

Running rails generate rspec:install creates a rails_helper.rb file that includes a line like this – although it may be commented out by default. The other option is to manually require support files that you need in each *_spec.rb file.

The spec/helpers/ directory, on the other hand, is generally used for specs that test your view helpers (much like model specs live in spec/models/ and controller specs live in spec/controllers/, etc.).

This support document about rspec directory structure lists other conventions that may be helpful, too.

2 Likes
(Michael Merrill) #3

Great response. Thank you for the clarification!