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.

On Jan 1, 8:34 am, bdeverea <bdeve...@gmail.com> wrote:
> 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 Cheung <frederick.che...@gmail.com>
> wrote:
>
> > On 1 Jan 2009, at 02:13, bdeverea <bdeve...@gmail.com> 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
>
> > > 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!
--~--~---------~--~----~------------~-------~--~----~
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@googlegroups.com
To unsubscribe from this group, send email to 
rubyonrails-talk+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to