Model validation breaking integration test


#1

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!


#2

On 1 Jan 2009, at 02:13, bdeverea 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


#3

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


#4

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.