Read more

RSpec: Where to put custom matchers and other support code

Henning Koch
August 20, 2013Software engineer at makandra GmbH

Custom matchers Show archive.org snapshot are a useful RSpec feature which you can use to DRY up repetitive expectations in your specs. Unfortunately the default directory structure generated by rspec-rails has no obvious place to put custom matchers or other support code.

Illustration online protection

Rails Long Term Support

Rails LTS provides security patches for old versions of Ruby on Rails (2.3, 3.2, 4.2 and 5.2)

  • Prevents you from data breaches and liability risks
  • Upgrade at your own pace
  • Works with modern Rubies
Read more Show archive.org snapshot

I recommend storing them like this:

spec/support/database_cleaner.rb
spec/support/devise.rb
spec/support/factory_bot.rb
spec/support/vcr.rb
spec/support/matchers/be_allowed_access.rb
spec/support/matchers/be_denied_access.rb
spec/support/matchers/be_same_second_as.rb

To make this support code available to all specs, put the following into your spec_helper.rb, above the RSpec.configure block:

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

Also see where to put shared example groups.

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