Restore uniqueness scope values after verifying each scope.

A test for a validation with multiple scopes would always pass as long
as there was a validation with the first scope. Subsequent scopes would
pass verification because the first scope value had already been made
unique.
This commit is contained in:
Ian Lesperance 2012-06-27 17:57:13 -07:00 committed by Mike Burns
parent 35b65c2bc3
commit cb02954e57
2 changed files with 15 additions and 4 deletions

View File

@ -133,6 +133,8 @@ module Shoulda # :nodoc:
@subject.send("#{scope}=", next_value)
if allows_value_of(existing_value, @expected_message)
@subject.send("#{scope}=", previous_value)
@negative_failure_message <<
" (with different value of #{scope})"
true

View File

@ -66,11 +66,12 @@ describe Shoulda::Matchers::ActiveModel::ValidateUniquenessOfMatcher do
before do
@model = define_model(:example, :attr => :string,
:scope1 => :integer,
:scope2 => :integer) do
attr_accessible :attr, :scope1, :scope2
:scope2 => :integer,
:other => :integer) do
attr_accessible :attr, :scope1, :scope2, :other
validates_uniqueness_of :attr, :scope => [:scope1, :scope2]
end.new
@existing = Example.create!(:attr => 'value', :scope1 => 1, :scope2 => 2)
@existing = Example.create!(:attr => 'value', :scope1 => 1, :scope2 => 2, :other => 3)
end
it "should pass when the correct scope is specified" do
@ -81,10 +82,18 @@ describe Shoulda::Matchers::ActiveModel::ValidateUniquenessOfMatcher do
@existing.should validate_uniqueness_of(:attr).scoped_to(:scope1, :scope2)
end
it "should fail when a different scope is specified" do
it "should fail when too narrow of a scope is specified" do
@model.should_not validate_uniqueness_of(:attr).scoped_to(:scope1, :scope2, :other)
end
it "should fail when too broad of a scope is specified" do
@model.should_not validate_uniqueness_of(:attr).scoped_to(:scope1)
end
it "should fail when a different scope is specified" do
@model.should_not validate_uniqueness_of(:attr).scoped_to(:other)
end
it "should fail when no scope is specified" do
@model.should_not validate_uniqueness_of(:attr)
end