Setup Shoulda matchers in Rails
11 March 2022 (Updated 11 March 2022)
On this page
Install gem
Add to Gemfile:
group :test do
gem 'shoulda-matchers', '~> 5.0'
end
Run:
./bin/bundle install
Configure with RSpec
Create spec/support/shoulda_matchers.rb
:
# frozen_string_literal: true
Shoulda::Matchers.configure do |config|
config.integrate do |with|
with.test_framework :rspec
with.library :rails
end
end
Make sure this file is included from your rails_helper.rb
or spec_helper.rb
.
Use in tests
For example, a spec/models/user_spec.rb
file might have:
# frozen_string_literal: true
require 'rails_helper'
RSpec.describe User, type: :model do
describe 'validations' do
it { should validate_presence_of(:email) }
it { should validate_presence_of(:password) }
end
end
Sources
Tagged:
Rails testing
Thanks for your comment 🙏. Once it's approved, it will appear here.
Leave a comment