This guides you through adding the test expectation-matching utility shoulda-matchers to your Ruby on Rails RSpec project.
To install shoulda-matchers you will need to...
- Add the shoulda-matchers gem to your Rails RSpec project
- Configure shoulda-matchers to integrate with RSpec Rails
You need to add the shoulda-matchers
gem to your Gemfile
but really only need it for test.
-
Edit your
Gemfile
and addgem 'shoulda-matchers'
to just:test
group
, for example...group :test do gem 'shoulda-matchers' end
-
Run
bundle install
to install theshoulda-matchers
gembundle install
☁️ If you are doing container-native development, be sure to run your
bundle
commands in the container environment
You need to configure shoulda-matchers to use RSpec Rails as the test framework.
You can add the shoulda-matchers configuration directly to your
rails_helper
file, but you may find it cleaner to create a
separate file and include it.
-
Create a new
spec/support/config/
directory if you do not already have onemkdir -p spec/support/config
-
Create a new
spec/support/config/shoulda-matchers.rb
filetouch spec/support/config/shoulda-matchers.rb
-
Edit your
spec/support/config/shoulda-matchers.rb
file to# frozen_string_literal: true Shoulda::Matchers.configure do |config| config.integrate do |with| with.test_framework :rspec with.library :rails end end
-
Save your edited
spec/support/config/shoulda-matchers.rb
file -
Edit your
spec/rails_helper.rb
file and addrequire 'support/config/shoulda-matchers
, for example...# Add additional requires below this line. Rails is not loaded until this point! require 'support/config/shoulda-matchers'
-
Save your edited
spec/rails_helper.rb
file
🏃 You should now be able to use the shoulda-matchers in your Rails specs.