Example:
validate_uniqueness_of
matcher
Post model
class Post < ActiveRecord::Base
validates_uniqueness_of :slug,
scope: :user_id,
message: 'duplicate slug within same user_id',
case_insensitive: true
end
Test using RSpec
describe Post do
context 'validations' do
subject { FactoryBot.build(:post) }
it do
should validate_uniqueness_of(:slug).
scoped_to(:user_id).
with_message('duplicate slug within same user_id').
case_insensitive
end
end
end
Test using Shoulda Context
class PostTest < ActiveSupport::TestCase
context 'validations' do
subject { FactoryBot.build(:post) }
should validate_uniqueness_of(:slug).
scoped_to(:user_id).
with_message('duplicate slug within same user_id').
case_insensitive
end
end