Checking for duplicate values across multiple fields with case-insensitive
match
I have two fields, name and email, whose combination should be unique
without regard for case. Here is code for class and specs. The first 3
tests pass, but not the 4th one.
class Person < ActiveRecord::Base
validates :name, presence: true
validates :email, presence: true
validates_uniqueness_of :email, :scope => :name, :case_sensitive => false
validates_uniqueness_of :name, :scope => :email, :case_sensitive => false
end
describe Person do
context "with duplicate name and email" do
before do
@person1 = create(:person)
end
it "for case-sensitive match of both" do
expect(build(:person, {name: @person1.name, email:
@person1.email})).to_not be_valid
end
it "for case-insensitive match of name" do
expect(build(:person, {name: @person1.name.swapcase, email:
@person1.email})).to_not be_valid
end
it "for case-insensitive match of email" do
expect(build(:person, {name: @person1.name, email:
@person1.email.swapcase})).to_not be_valid
end
it "for case-insensitive match of both" do
expect(build(:person, {name: @person1.name.swapcase, email:
@person1.email.swapcase})).to_not be_valid
end
end
end
No comments:
Post a Comment