Read more

RSpec: Where to put shared example groups

Henning Koch
August 20, 2013Software engineer at makandra GmbH

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

Illustration web development

Do you need DevOps-experts?

Your development team has a full backlog? No time for infrastructure architecture? Our DevOps team is ready to support you!

  • We build reliable cloud solutions with Infrastructure as code
  • We are experts in security, Linux and databases
  • We support your dev team to perform
Read more Show archive.org snapshot

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.

Posted by Henning Koch to makandra dev (2013-08-20 11:35)