# Functional test a "create" action when a validation fails

**URL:** <https://www.ruby-forum.com/t/functional-test-a-create-action-when-a-validation-fails/80830>\
**Category:** Rails\
**Created:** [December 31, 2006, 12:40am UTC](https://www.ruby-forum.com/t/functional-test-a-create-action-when-a-validation-fails/80830 "2006-12-31T00:40:45Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![kendall](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/9dc877/32.png) [@kendall](https://www.ruby-forum.com/u/kendall)\
**Post date:** [December 31, 2006, 12:40am UTC](https://www.ruby-forum.com/t/functional-test-a-create-action-when-a-validation-fails/80830/1 "2006-12-31T00:40:45Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![kendall](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/9dc877/32.png) [@kendall](https://www.ruby-forum.com/u/kendall)\
**Post date:** [December 31, 2006, 1:28pm UTC](https://www.ruby-forum.com/t/functional-test-a-create-action-when-a-validation-fails/80830/2 "2006-12-31T13:28:23Z")

</div>

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 S. - [http://mentalized.net](http://mentalized.net)

---

<div class="post-metadata">

**Author:** ![kendall](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/9dc877/32.png) [@kendall](https://www.ruby-forum.com/u/kendall)\
**Post date:** [December 31, 2006, 2:34pm UTC](https://www.ruby-forum.com/t/functional-test-a-create-action-when-a-validation-fails/80830/3 "2006-12-31T14:34:38Z")

</div>

Thank you. I’ll try that and report back.

---

<div class="post-metadata">

**Author:** ![kendall](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/9dc877/32.png) [@kendall](https://www.ruby-forum.com/u/kendall)\
**Post date:** [January 1, 2007, 6:29pm UTC](https://www.ruby-forum.com/t/functional-test-a-create-action-when-a-validation-fails/80830/4 "2007-01-01T18:29:40Z")

</div>

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
