I''m trying to write a functional test of a "create" action in one of my controllers. In the corresponding model I have a "validates_uniqueness_of :location". In the test case I am constructing I have the create fail with the error "Location has already been taken". Is there a way I can write an assertion like: assert_equal "Location has already been taken", [the_actual_error]. I can''t figure out how to retrieve the validation errors. Thanks. --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Jakob Skjerning
2006-Dec-31 12:27 UTC
Re: Functional test a "create" action when a validation fails
kendall wrote:> > I''m trying to write a functional test of a "create" action in one of my > controllers. In the corresponding model I have a > "validates_uniqueness_of :location". In the test case I am > constructing I have the create fail with the error "Location has > already been taken". Is there a way I can write an assertion like: > assert_equal "Location has already been taken", [the_actual_error]. I > can''t figure out how to retrieve the validation errors. Thanks.assert_equal "has already been taken", assigns(:your_model_instance).errors(:location) If my memory fails me and the above doesn''t work, check the API reference for ActiveRecord::Errors for the proper methods. -- Jakob Skjerning - http://mentalized.net --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
kendall
2006-Dec-31 13:31 UTC
Re: Functional test a "create" action when a validation fails
Thank you. I''ll try that and report back. --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
kendall
2007-Jan-01 17:27 UTC
Re: Functional test a "create" action when a validation fails
Thanks, Jakob, that was good advice. I looked at the ActiveRecord::Errors API and saw that it provides a ".on" method that returns the error message text when provided with the attribute, which in this case I assume would be "location_id". Here is some code that works: assert_equal ''has already been taken'', assigns(:my_model_instance).errors.on(''location_id'') This next one also works. Not sure which is better, but I think I''ll keep the latter one for now. assert_equal ActiveRecord::Errors.default_error_messages[:taken], assigns(:my_model_instance).errors.on(''location_id'') Appreciate your help, Kendall --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---