Posted almost 9 years ago. Visible to the public.

RSpec: Where to put shared example groups

Shared example groups Archive are a useful RSpec feature. Unfortunately the default directory structure generated by rspec-rails has no obvious place to put them.

I recommend storing them like this:

spec/models/shared_examples/foo.rb spec/models/shared_examples/bar.rb spec/models/shared_examples/baz.rb spec/controllers/shared_examples/foo.rb spec/controllers/shared_examples/bar.rb spec/controllers/shared_examples/baz.rb

To make those shared examples available to all specs, put the following into your spec_helper.rb (for rails 4 in rails_helper.rb), above the RSpec.configure block:

Dir[Rails.root.join("spec/models/shared_examples/**/*.rb")].each {|f| require f} Dir[Rails.root.join("spec/controllers/shared_examples/**/*.rb")].each {|f| require f}

Make sure you don't call your examples files like ..._spec.rb, else RSpec will believe they're actual spec files and require them.

Also see where to put custom matchers and other support code.

Does your version of Ruby on Rails still receive security updates?
Rails LTS provides security patches for unsupported versions of Ruby on Rails (2.3, 3.2, 4.2 and 5.2).

Owner of this card:

Henning Koch
Last edit:
almost 7 years ago
by Dominik Schöler
store, save, folder, location, path
About this deck:
We are makandra and do test-driven, agile Ruby on Rails software development.
License for source code
Posted by Henning Koch to makandra dev
This website uses short-lived cookies to improve usability.
Accept or learn more