# Testing error messages on objects with multiple validations

**URL:** <https://www.ruby-forum.com/t/testing-error-messages-on-objects-with-multiple-validations/123378>\
**Category:** Rails\
**Created:** [December 17, 2007, 7:28am UTC](https://www.ruby-forum.com/t/testing-error-messages-on-objects-with-multiple-validations/123378 "2007-12-17T07:28:46Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![mstreffo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/c0e974/32.png) [@mstreffo](https://www.ruby-forum.com/u/mstreffo)\
**Post date:** [December 17, 2007, 7:28am UTC](https://www.ruby-forum.com/t/testing-error-messages-on-objects-with-multiple-validations/123378/1 "2007-12-17T07:28:46Z")

</div>

I have 2 validations on a single object, like this in my user model:

validates\_length\_of :password, :within =\> 6…12  
validates\_presence\_of :password

When I do a unit test to ensure a missing password is responded to  
correctly, for example:

@error\_messages = ActiveRecord::Errors.default\_error\_messages  
user = User.new(:name =\> ‘my test user’,  
assert !user.valid?  
assert\_equal @error\_messages[:blank], user.errors.on(:password)

I get a message such as:

test\_create\_user\_passwords\_missing(UserTest)  
[test/unit/user\_test.rb:143]:  
\<“can’t be blank”\> expected but was  
\<[“is too short (minimum is 6 characters)”, “can’t be blank”]\>.

I have 2 questions:

1. Is there a way of ensuring this test passes, ie. by checking for  
both  
validations at once?
2. How can I surpress or change the display of these messages in the  
views? For example, if I get the “can’t be blank” and the “is too  
short…” message, I would like to display a message saying “Please  
ensure you enter a password between 6 and 12 characters in length”.

All help appreciated!

Thanks

Mark.

---

<div class="post-metadata">

**Author:** ![mstreffo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/c0e974/32.png) [@mstreffo](https://www.ruby-forum.com/u/mstreffo)\
**Post date:** [December 17, 2007, 9:15am UTC](https://www.ruby-forum.com/t/testing-error-messages-on-objects-with-multiple-validations/123378/2 "2007-12-17T09:15:26Z")

</div>

You can use :message to give your message like below:

validates\_presence\_of :password, :message =\> “your msg”

---

<div class="post-metadata">

**Author:** ![mstreffo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/c0e974/32.png) [@mstreffo](https://www.ruby-forum.com/u/mstreffo)\
**Post date:** [December 17, 2007, 9:28am UTC](https://www.ruby-forum.com/t/testing-error-messages-on-objects-with-multiple-validations/123378/3 "2007-12-17T09:28:04Z")

</div>

Use Array#include?.

assert user.errors.on(:password).include?(@error\_messages[:blank])

On Dec 17, 9:15 am, Ayyanar Aswathaman \<rails-mailing-l…@andreas-
