# Model validation breaking integration test

**URL:** <https://www.ruby-forum.com/t/model-validation-breaking-integration-test/156689>\
**Category:** Rails\
**Created:** [January 1, 2009, 3:13am UTC](https://www.ruby-forum.com/t/model-validation-breaking-integration-test/156689 "2009-01-01T03:13:35Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![bdeverea](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/f6c823/32.png) [@bdeverea](https://www.ruby-forum.com/u/bdeverea)\
**Post date:** [January 1, 2009, 3:13am UTC](https://www.ruby-forum.com/t/model-validation-breaking-integration-test/156689/1 "2009-01-01T03:13:35Z")

</div>

Greetings,  
I have a simple model called “Section” with integration tests that  
pass. However, when I add a simple validation to the model  
(validates\_presence\_of :name) the integration test fails. This makes  
no sense to me because I am passing in a name.

Does anyone know why that validation breaks my integration test?

INTEGRATION TEST

require “#{File.dirname( **FILE** )}/…/test\_helper”

class SectionsTest \< ActionController::IntegrationTest

fixtures :sections

def test\_user\_creates\_new\_section  
#Navigate to new Section form  
get ‘/sections/new’  
assert\_response :success

```
#Submit new Section form with params
post '/sections/create', :name => sections(:Home).name
assert_response :redirect
follow_redirect!

#Test that Section was created successfully and user was

```

redirected to show page  
assert\_template ‘sections/show’  
assert\_equal ‘Section was successfully created.’, flash[:notice]

end  
end

MODEL CODE  
class Section \< ActiveRecord::Base  
has\_many :pages

before\_create :set\_create\_user  
before\_save :set\_modified\_user

validates\_presence\_of :name #Adding this line causes the integration  
test above fail  
validates\_uniqueness\_of :name

private

def set\_create\_user  
self.created\_by = 1  
end

def set\_modified\_user  
self.modified\_by = 2  
end  
end

Thanks for any help you can provide!

---

<div class="post-metadata">

**Author:** ![bdeverea](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/f6c823/32.png) [@bdeverea](https://www.ruby-forum.com/u/bdeverea)\
**Post date:** [January 1, 2009, 10:07am UTC](https://www.ruby-forum.com/t/model-validation-breaking-integration-test/156689/2 "2009-01-01T10:07:58Z")

</div>

On 1 Jan 2009, at 02:13, bdeverea [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Greetings,  
> I have a simple model called “Section” with integration tests that  
> pass. However, when I add a simple validation to the model  
> (validates\_presence\_of :name) the integration test fails. This makes  
> no sense to me because I am passing in a name.
> 
> Does anyone know why that validation breaks my integration test?

Because you’ve also got a validates\_uniqueness\_of and you’re reusing  
the name of an existing section.

Fred

---

<div class="post-metadata">

**Author:** ![bdeverea](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/f6c823/32.png) [@bdeverea](https://www.ruby-forum.com/u/bdeverea)\
**Post date:** [January 1, 2009, 2:35pm UTC](https://www.ruby-forum.com/t/model-validation-breaking-integration-test/156689/3 "2009-01-01T14:35:29Z")

</div>

Thanks for the reply, Frederick.

However, if I remove the validates\_presence\_of validation then the  
Section can be saved without a name. Is there a different way I should  
validating that the :name exists and is :unique?

Thanks again.  
-B

On Jan 1, 4:06 am, Frederick C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![bdeverea](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/f6c823/32.png) [@bdeverea](https://www.ruby-forum.com/u/bdeverea)\
**Post date:** [January 1, 2009, 2:39pm UTC](https://www.ruby-forum.com/t/model-validation-breaking-integration-test/156689/4 "2009-01-01T14:39:38Z")

</div>

Oh, I misunderstood your reply. My apologies. I now see that you’re  
saying that the validates\_uniqueness\_of is the validation that is  
throwing the error because I am using a Section from the Fixtures.

However, if I simply remove the validates\_presence\_of validation it  
works fine. In addition, it fails even if I don’t use the fixtures and  
simply enter in a random string for the :name value.
