# Model validates twice in tests, produces duplicate errors

**URL:** <https://www.ruby-forum.com/t/model-validates-twice-in-tests-produces-duplicate-errors/93545>\
**Category:** Rails\
**Created:** [April 4, 2006, 4:55pm UTC](https://www.ruby-forum.com/t/model-validates-twice-in-tests-produces-duplicate-errors/93545 "2006-04-04T16:55:43Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![natec](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/ec9cab/32.png) [@natec](https://www.ruby-forum.com/u/natec)\
**Post date:** [April 4, 2006, 4:55pm UTC](https://www.ruby-forum.com/t/model-validates-twice-in-tests-produces-duplicate-errors/93545/1 "2006-04-04T16:55:43Z")

</div>

In my unit tests for my User model, I’m testing some validation cases.  
What is really strange, and driving me crazy, is that in the unit  
tests, it seems like the save method is causing my validations to  
execute twice, and produce duplicate error messages. This is making my  
tests fail (because I’m checking for the number of errors that I  
expect). In the web browser, only one error message is displayed.  
Anyone know what the cause of this could be?

The Model:

validates\_length\_of :zip\_code, :is =\> 5, :allow\_nil =\> true

The Unit Test:

def test\_create\_user\_with\_invalid\_zip\_code  
@u = User.new(  
:password =\> ‘asecurepassword’,  
:password\_confirmation =\> ‘asecurepassword’,  
:display\_name =\> ‘invalid zip’,  
:email =\> ‘removed\_email\_address@domain.invalid’,  
:zip\_code =\> ‘265’  
)  
@u.new\_password = true  
assert !removed\_email\_address@domain.invalid  
assert\_equal 1, @u.errors.count  
assert\_equal “is the wrong length (should be 5 characters)”,  
u.errors.on(:zip\_code)  
end

the test result:

1. Failure:  
test\_create\_user\_with\_invalid\_zip\_code(UserTest)  
[test/unit/user\_test.rb:112]:  
\<1\> expected but was  
\<2\>.

further inspection of the @u instance:

=\> #\<User:0x38e0598 @password=“asecurepassword”,  
@attributes={“salt”=\>"", “delete\_after”=\>nil, “updated\_at”=\>nil,  
“security\_token”=\>nil, “role”=\>nil, “country”=

> nil, “zip\_code”=\>“265”, “gender”=\>nil, “token\_expiry”=\>nil, “is\_verified”=\>0, “birthday”=\>nil, “is\_deleted”=\>0, “display\_name”=\>“invalid zip”, “logged\_in\_at”=\>nil, “salted\_password”=\>"", “created\_at”=\>nil, “email”=\>“removed\_email\_address@domain.invalid”}, @password\_confirmation=“asecurepassword”, @new\_password=false, @errors=#\<ActiveRecord::Errors:0x38b7768 @base=#\<User:0x38e0598 …\>, @errors={“zip\_code”=\>[“is the wrong length (should be 5 characters)”, “is the wrong length (should be 5 characters)”]}\>, @new\_record=true\>

please help! this is driving me nuts

---

<div class="post-metadata">

**Author:** ![natec](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/ec9cab/32.png) [@natec](https://www.ruby-forum.com/u/natec)\
**Post date:** [April 4, 2006, 9:09pm UTC](https://www.ruby-forum.com/t/model-validates-twice-in-tests-produces-duplicate-errors/93545/2 "2006-04-04T21:09:35Z")

</div>

I’ve been spending all day (well, most of the day) trying to figure this  
out, I can’t seem to determine how to do a “step” type of debugging  
routine. What is the best way to debug Ruby on Rails?

I have been using breakpointer and irb, but that only allows me to stop  
the execution at a certain point and then run commands manually. Is  
there a way to make a breakpoint and then step into to see what’s going  
on?

For example, I’d like to step into @u.save and see what other methods  
are called before it actually saves. There’s gotta be a way, right? I’m  
using Radrails mostly to develop … but I guess the debugger doesn’t  
really work yet?

---

<div class="post-metadata">

**Author:** ![natec](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/ec9cab/32.png) [@natec](https://www.ruby-forum.com/u/natec)\
**Post date:** [April 8, 2006, 1:38am UTC](https://www.ruby-forum.com/t/model-validates-twice-in-tests-produces-duplicate-errors/93545/3 "2006-04-08T01:38:25Z")

</div>

I’m also having a problem similar to this. I a using the current Rails  
gems, 1.1.1.

It seems the number of errors is double in the test, but not in the  
console. Any suggestions?

Here’s the evidence:

def test\_parent\_validation  
# new stepTypes require parent  
st = StepType.new(:name =\> ‘Test’, :parent\_id =\> ‘soup’ )  
assert !st.save  
assert\_equal 1, st.errors.count, st.errors.full\_messages #  
st.errors.on(:parent\_id)  
end

yields …

1. Failure:  
test\_parent\_validation(StepTypeTest) [test/unit/step\_type\_test.rb:74]:  
Parent is not a numberParent is not a number.  
\<1\> expected but was  
\<2\>.

But in script/console test …

Loading test environment.

> > st = StepType.new(:name =\> ‘Test’, :parent\_id =\> ‘soup’ )  
> > =\> #\<StepType:0x3e1a070 @attributes={“class\_name”=\>nil, “name”=\>“Test”,  
> > “parent\_  
> > id”=\>“soup”}, @new\_record=true\>
> 
> > !st.save  
> > =\> true
> 
> > st.errors.count  
> > =\> 1
> 
> > st.errors.full\_messages  
> > =\> [“Parent is not a number”]

Nate C. wrote:

> What is really strange, and driving me crazy, is that in the unit  
> tests, it seems like the save method is causing my validations to  
> execute twice, and produce duplicate error messages.

---

<div class="post-metadata">

**Author:** ![natec](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/ec9cab/32.png) [@natec](https://www.ruby-forum.com/u/natec)\
**Post date:** [April 8, 2006, 8:22pm UTC](https://www.ruby-forum.com/t/model-validates-twice-in-tests-produces-duplicate-errors/93545/4 "2006-04-08T20:22:52Z")

</div>

I’m glad I’m not the only one … I still haven’t figured it out. Do you  
think it’s a bug in Rails? I’m on 1.1.

Sam S - M wrote:

> I’m also having a problem similar to this. I a using the current Rails  
> gems, 1.1.1.
