I use rspec-rails with shoulda-matcher to test my model. Here is the code:
user_ticket.rb
class UserTicket < ActiveRecord::Base
belongs_to :user
belongs_to :ticket
enum relation_type: %w( creator supporter )
validates_uniqueness_of :relation_type, scope: [:user_id, :ticket_id]
end
user_ticket_spec.rb
RSpec.describe UserTicket, type: :model do
subject { FactoryGirl.build(:user_ticket) }
describe 'Relations' do
it { should belong_to(:user) }
it { should belong_to(:ticket) }
end
describe 'Validations' do
it { should define_enum_for(:relation_type).with(%w( creator supporter )) }
# PROBLEM HERE
it { should validate_uniqueness_of(:relation_type).case_insensitive.scoped_to([:user_id, :ticket_id]) }
end
end
When I run the test case the result is always:
Failure/Error: it { should validate_uniqueness_of(:relation_type).case_insensitive.scoped_to([:user_id, :ticket_id]) }
ArgumentError:
'CREATOR' is not a valid relation_type
I just think shoulda matcher wants to validate the uniqueness with some kinds of relation_type
values: uppercase, lowercase,..etc. My question is in this situation, how to make the test pass with such of defined model validations?
validates_uniqueness_of :type, scope: :user_id
, unfortunately this check is not working in this case:it { is_expected.to validate_uniqueness_of(:type).scoped_to(:user_id) }
. So need to extendshoulda
or write the test manually – Inspectorate