require File.dirname(__FILE__) + '/../test_helper' require 'people_controller' # Re-raise errors caught by the controller. class PeopleController; def rescue_action(e) raise e end; end class PeopleControllerTest < Test::Unit::TestCase def setup @controller = PeopleController.new @request = ActionController::TestRequest.new @response = ActionController::TestResponse.new end # Test that invalid person parameters fail to create a person # and throw validation errors. def test_create_bad_person post :create assert_template 'people/new' assert_select 'div[class=formError]' end # Test that valid person parameters and empty address # parameters create a person and redirects to index. def test_create_person_nil_address # Make sure we've cleared out the person table Person.delete_all # Post new person details with blank address post :create, :person => { :first_name => 'Bob', :last_name => 'Parks', :email => 'bob@acme.biz', :gender => 'M' } # Check there is one person in the database person = Person.find(:first) assert_equal 'bob@acme.biz', person.email # Check their address is nil assert_equal nil, person.address # Check we get redirected to the index assert_redirected_to :action => :index end def test_create_bad_street_1 # Post valid person details with bad street_1 post :create, :person => { :first_name => 'Bob', :last_name => 'Parks', :email => 'bob@acme.biz', :gender => 'M' }, :address => { :post_code => 'B15 1AU' } # Check the form is shown again assert_template 'people/new' # Check we get validation errors for street_1 input element assert_select 'div[class=fieldWithErrors]' do assert_select 'input[id=address_street_1]' end end def test_create_bad_post_code # Post valid person details with bad street_1 post :create, :person => { :first_name => 'Bob', :last_name => 'Parks', :email => 'bob@acme.biz', :gender => 'M' }, :address => { :street_1 => '11 Harley Street' } # Check the form is shown again assert_template 'people/new' # Check we get validation errors for street_1 input element assert_select 'div[class=fieldWithErrors]' do assert_select 'input[id=address_post_code]' end end def test_create_person_address # Make sure we've cleared out the people and addresses tables Person.delete_all Address.delete_all # Send post with valid person and address post :create, :person => { :first_name => 'Bob', :last_name => 'Parks', :email => 'bob@acme.biz', :gender => 'M' }, :address => { :street_1 => '11 Harley Street', :post_code => 'B15 1AU' } # Check person created person = Person.find(:first) assert_equal 'bob@acme.biz', person.email # Check address created address = Address.find(:first) assert_equal 'B15 1AU', address.post_code # Check person's address is the created address assert_equal person.address_id, address.id # Check redirected to index assert_redirected_to :action => :index end end