Skip to content

Commit

Permalink
The validate_uniqueness_of matcher no longer fails when the subject i…
Browse files Browse the repository at this point in the history
…s an existing record
  • Loading branch information
jferris committed Jan 31, 2009
1 parent 02a9753 commit 44ceeb7
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ def description
end

def matches?(subject)
@subject = subject
@subject = subject.class.new
@expected_message ||= :taken
find_existing &&
set_scoped_attributes &&
Expand Down
13 changes: 11 additions & 2 deletions test/matchers/validate_uniqueness_of_matcher_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,17 @@ class ValidateUniquenessOfMatcherTest < Test::Unit::TestCase # :nodoc:

context "with an existing value" do
setup do
Example.create!(:attr => 'value', :other => 1)
@existing = Example.create!(:attr => 'value', :other => 1)
end

should "require a unique value for that attribute" do
assert_accepts validate_uniqueness_of(:attr), @model
end

should "pass when the subject is an existing record" do
assert_accepts validate_uniqueness_of(:attr), @existing
end

should "fail when a scope is specified" do
assert_rejects validate_uniqueness_of(:attr).scoped_to(:other), @model
end
Expand Down Expand Up @@ -63,14 +67,19 @@ class ValidateUniquenessOfMatcherTest < Test::Unit::TestCase # :nodoc:
:scope2 => :integer) do
validates_uniqueness_of :attr, :scope => [:scope1, :scope2]
end.new
Example.create!(:attr => 'value', :scope1 => 1, :scope2 => 2)
@existing = Example.create!(:attr => 'value', :scope1 => 1, :scope2 => 2)
end

should "pass when the correct scope is specified" do
assert_accepts validate_uniqueness_of(:attr).scoped_to(:scope1, :scope2),
@model
end

should "pass when the subject is an existing record" do
assert_accepts validate_uniqueness_of(:attr).scoped_to(:scope1, :scope2),
@existing
end

should "fail when a different scope is specified" do
assert_rejects validate_uniqueness_of(:attr).scoped_to(:scope1), @model
end
Expand Down

0 comments on commit 44ceeb7

Please sign in to comment.