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}

Cucumber

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}
Henning Koch About 13 years ago