mstreffo
2007-Dec-17 06:28 UTC
Testing error messages on objects with multiple validations
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. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Ayyanar Aswathaman
2007-Dec-17 08:15 UTC
Re: Testing error messages on objects with multiple validations
You can use :message to give your message like below: validates_presence_of :password, :message => "your msg" -- 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 post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
August Lilleaas
2007-Dec-17 08:26 UTC
Re: Testing error messages on objects with multiple validations
Use Array#include?. assert user.errors.on(:password).include?(@error_messages[:blank]) On Dec 17, 9:15 am, Ayyanar Aswathaman <rails-mailing-l...@andreas- s.net> wrote:> You can use :message to give your message like below: > > validates_presence_of :password, :message => "your msg" > -- > Posted viahttp://www.ruby-forum.com/.--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---