Posted over 8 years ago. Visible to the public. Repeats.

RSpec: Where to put custom matchers and other support code

Custom matchers Archive 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.

I recommend storing them like this:

Copy
spec/support/database_cleaner.rb spec/support/devise.rb spec/support/factory_bot.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:

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

Also see where to put shared example groups.

By refactoring problematic code and creating automated tests, makandra can vastly improve the maintainability of your Rails application.

Owner of this card:

Avatar
Henning Koch
Last edit:
about 1 month ago
by Emanuel De
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