Posted over 10 years ago. Visible to the public.

Where to put custom RSpec matchers

When you write a custom RSpec matcher a good place to store them is to create one file per matcher in spec/support/matchers:

spec/support/matchers/be_same_numbers_as.rb spec/support/matchers/be_same_second_as.rb spec/support/matchers/exist_in_database.rb spec/support/matchers/include_hash.rb

You can include all matchers in the support directory by adding the following line to your spec_helper.rb:

Dir[File.expand_path(File.join(File.dirname(__FILE__),'support','**','*.rb'))].each {|f| require f}


If you want to use your custom matchers in Cucumber scenarios, you need to require them in features/support/env.rb:

Dir[File.expand_path('spec/support/matchers/*.rb')].each {|f| require f}
Growing Rails Applications in Practice
Check out our new e-book:
Learn to structure large Ruby on Rails codebases with the tools you already know and love.

Owner of this card:

Henning Koch
Last edit:
over 6 years ago
by Dominik Schöler
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