I have a Private methods in my model like the following:
validate :record_uniq
private
def record_uniq
if record_already_exists?
errors.add(:base, "already exists")
end
end
def record_already_exists?
question_id = measure.question_id
self.class.joins(:measure).
where(measures: {question_id: ques_id}).
where(package_id: pack_id).
exists?
end
This methods is more like a uniqueness
scope thing to prevent duplicate records. I want to know how to write test for validate :record_uniq
by using shoulda
or rspec
?
Example of what i tried:
describe Foo do
before do
@bar = Foo.new(enr_rds_measure_id: 1, enr_rds_package_id: 2)
end
subject { @bar }
it { should validate_uniqueness_of(:record_uniq) }
end