[Rails] validates :uniqueness apparently doesn't
ruby 2.0.0p0 (2013-02-24 revision 39474) [x86_64-darwin12.3.0]
Rails 3.2.13
I have a validates ... :uniqueness constraint on one of the attributes
of an ActiveRecord class.
In my test suite, I set the attribute from the same attribute in a
record in the fixture. I then send invalid? to the object under test.
invalid? returns _false_, and the .errors object for the record shows no
errors.
A :uniqueness constraint on another attribute does invalidate the
record. All validations on other attributes behave as expected.
I've done what I could to search the Rails docs, Google, Stack Overflow,
and this forum for similar problems, and found nothing.
What's wrong?
=====
All code is excerpted.
Fixture:
active_1:
name: Thomas Jefferson
tracking_code: tj_001_TC01
...
Class declaration:
class Family < ActiveRecord::Base
attr_accessible ... :tracking_code
validates :tracking_code, :presence => true, :length => { :maximum =>
20, :minimum => 8 }, :uniqueness => true
Test case:
setup do
@copy = families(:active_1).dup
attrs = { :tracking_code => "copy01_ext", ... }
@copy.attributes = attrs
end
test "validate tracking" do
@copy.tracking_code = nil
assert @copy.invalid?, "Nil tracking code is illegal."
# Assertion does not fire.
@copy.tracking_code = families(:active_1).tracking_code
assert @copy.tracking_code == families(:active_1).tracking_code
@copy.save # Not sending "save" has no effect on the apparent bug
is_invalid = @copy.invalid?
assert is_invalid, "Duplicate tracking code should be invalid."
# Assertion fires
...
=====
--
Posted via http://www.ruby-forum.com/.
--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe@googlegroups.com.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
0 Comments:
Post a Comment
Subscribe to Post Comments [Atom]
<< Home