# Validates\_uniqueness\_of does not pass unit test

**URL:** <https://www.ruby-forum.com/t/validates-uniqueness-of-does-not-pass-unit-test/105266>\
**Category:** Rails\
**Created:** [May 28, 2007, 9:04am UTC](https://www.ruby-forum.com/t/validates-uniqueness-of-does-not-pass-unit-test/105266 "2007-05-28T09:04:37Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![jko170](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/d07c76/32.png) [@jko170](https://www.ruby-forum.com/u/jko170)\
**Post date:** [May 28, 2007, 9:04am UTC](https://www.ruby-forum.com/t/validates-uniqueness-of-does-not-pass-unit-test/105266/1 "2007-05-28T09:04:37Z")

</div>

I am doing some very very basic unit testing. If I take away the  
uniqueness validation, the tests pass. Could this be a bug in Edge  
Rails?

---

<div class="post-metadata">

**Author:** ![jko170](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/d07c76/32.png) [@jko170](https://www.ruby-forum.com/u/jko170)\
**Post date:** [May 28, 2007, 9:20am UTC](https://www.ruby-forum.com/t/validates-uniqueness-of-does-not-pass-unit-test/105266/2 "2007-05-28T09:20:39Z")

</div>

Hi, could you post the test?

Thanks,

-Conrad

---

<div class="post-metadata">

**Author:** ![jko170](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/d07c76/32.png) [@jko170](https://www.ruby-forum.com/u/jko170)\
**Post date:** [May 28, 2007, 9:36am UTC](https://www.ruby-forum.com/t/validates-uniqueness-of-does-not-pass-unit-test/105266/3 "2007-05-28T09:36:40Z")

</div>

In my model:  
validates\_uniqueness\_of :number, :scope =\> :school\_id

Unit test:  
def test\_student\_CRUD  
student = Student.new :school\_id =\> @john.school\_id, :number =\>  
@john.number

```
assert student.save
student_copy = Student.find(student.id)
assert_equal student.number, student_copy.number
student.number = "7777"
assert student.save
assert student.destroy

```

end

Fixture:  
john\_doe:  
id: 1  
school\_id: 1  
account\_id: 1  
family\_id: NULL  
first\_name: John  
last\_name: Doe  
number: 9999

Thanks for any help!

---

<div class="post-metadata">

**Author:** ![jko170](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/d07c76/32.png) [@jko170](https://www.ruby-forum.com/u/jko170)\
**Post date:** [May 29, 2007, 2:48am UTC](https://www.ruby-forum.com/t/validates-uniqueness-of-does-not-pass-unit-test/105266/4 "2007-05-29T02:48:33Z")

</div>

So what actually happens when you run this test, and what are you  
expecting it to do?

My reading says that it should fail on the first student.save line.  
(And the test will, of course, pass if you remove the uniqueness  
validation.)

---

<div class="post-metadata">

**Author:** ![jko170](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/d07c76/32.png) [@jko170](https://www.ruby-forum.com/u/jko170)\
**Post date:** [May 29, 2007, 3:39am UTC](https://www.ruby-forum.com/t/validates-uniqueness-of-does-not-pass-unit-test/105266/5 "2007-05-29T03:39:38Z")

</div>

You are correct, it fails on the on the save. If I remove it, it does  
pass. I want it to pass with the validation.

---

<div class="post-metadata">

**Author:** ![jko170](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/d07c76/32.png) [@jko170](https://www.ruby-forum.com/u/jko170)\
**Post date:** [May 29, 2007, 8:11pm UTC](https://www.ruby-forum.com/t/validates-uniqueness-of-does-not-pass-unit-test/105266/6 "2007-05-29T20:11:19Z")

</div>

Why does it fail on the student.save line? the number is unique as  
there are no other student fixtures.
