Model validates twice in tests, produces duplicate errors

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 => ‘[email protected]’,
:zip_code => ‘265’
)
@u.new_password = true
assert [email protected]
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”=>“[email protected]”}, @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

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?

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.

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.