Forum: Ruby on Rails Model validation breaking integration test

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Bryan D. (Guest)
on 2009-01-01 04:13
(Received via mailing list)
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!
Frederick C. (Guest)
on 2009-01-01 11:07
(Received via mailing list)
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
Bryan D. (Guest)
on 2009-01-01 15:35
(Received via mailing list)
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>
Bryan D. (Guest)
on 2009-01-01 15:39
(Received via mailing list)
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.
This topic is locked and can not be replied to.