nested attributes tests should rely on associated objects to verify results not on assert_difference [#5206 state:resolved]

Signed-off-by: José Valim <jose.valim@gmail.com>
This commit is contained in:
Subba Rao Pasupuleti 2010-07-26 23:09:39 -04:00 committed by José Valim
parent 46c14a6b03
commit dba4efbd0e

@ -74,9 +74,9 @@ def test_should_disable_allow_destroy_by_default
pirate = Pirate.create!(:catchphrase => "Don' botharrr talkin' like one, savvy?")
ship = pirate.create_ship(:name => 'Nights Dirty Lightning')
assert_no_difference('Ship.count') do
pirate.update_attributes(:ship_attributes => { '_destroy' => true, :id => ship.id })
end
assert_nothing_raised(ActiveRecord::RecordNotFound) { pirate.ship.reload }
end
def test_a_model_should_respond_to_underscore_destroy_and_return_if_it_is_marked_for_destruction
@ -194,28 +194,30 @@ def test_should_modify_an_existing_record_if_there_is_a_matching_composite_id
def test_should_destroy_an_existing_record_if_there_is_a_matching_id_and_destroy_is_truthy
@pirate.ship.destroy
[1, '1', true, 'true'].each do |truth|
@pirate.reload.create_ship(:name => 'Mister Pablo')
assert_difference('Ship.count', -1) do
@pirate.update_attributes(:ship_attributes => { :id => @pirate.ship.id, :_destroy => truth })
end
ship = @pirate.reload.create_ship(:name => 'Mister Pablo')
@pirate.update_attributes(:ship_attributes => { :id => ship.id, :_destroy => truth })
assert_nil @pirate.reload.ship
assert_raise(ActiveRecord::RecordNotFound) { Ship.find(ship.id) }
end
end
def test_should_not_destroy_an_existing_record_if_destroy_is_not_truthy
[nil, '0', 0, 'false', false].each do |not_truth|
assert_no_difference('Ship.count') do
@pirate.update_attributes(:ship_attributes => { :id => @pirate.ship.id, :_destroy => not_truth })
end
assert_equal @ship, @pirate.reload.ship
end
end
def test_should_not_destroy_an_existing_record_if_allow_destroy_is_false
Pirate.accepts_nested_attributes_for :ship, :allow_destroy => false, :reject_if => proc { |attributes| attributes.empty? }
assert_no_difference('Ship.count') do
@pirate.update_attributes(:ship_attributes => { :id => @pirate.ship.id, :_destroy => '1' })
end
assert_equal @ship, @pirate.reload.ship
Pirate.accepts_nested_attributes_for :ship, :allow_destroy => true, :reject_if => proc { |attributes| attributes.empty? }
end
@ -236,12 +238,15 @@ def test_should_work_with_update_attributes_as_well
end
def test_should_not_destroy_the_associated_model_until_the_parent_is_saved
assert_no_difference('Ship.count') do
@pirate.attributes = { :ship_attributes => { :id => @ship.id, :_destroy => '1' } }
end
assert_difference('Ship.count', -1) do
assert !@pirate.ship.destroyed?
assert @pirate.ship.marked_for_destruction?
@pirate.save
end
assert @pirate.ship.destroyed?
assert_nil @pirate.reload.ship
end
def test_should_automatically_enable_autosave_on_the_association
@ -254,29 +259,30 @@ def test_should_accept_update_only_option
def test_should_create_new_model_when_nothing_is_there_and_update_only_is_true
@ship.delete
assert_difference('Ship.count', 1) do
@pirate.reload.update_attributes(:update_only_ship_attributes => { :name => 'Mayflower' })
end
assert_not_nil @pirate.ship
end
def test_should_update_existing_when_update_only_is_true_and_no_id_is_given
@ship.delete
@ship = @pirate.create_update_only_ship(:name => 'Nights Dirty Lightning')
assert_no_difference('Ship.count') do
@pirate.update_attributes(:update_only_ship_attributes => { :name => 'Mayflower' })
end
assert_equal 'Mayflower', @ship.reload.name
assert_equal @ship, @pirate.reload.ship
end
def test_should_update_existing_when_update_only_is_true_and_id_is_given
@ship.delete
@ship = @pirate.create_update_only_ship(:name => 'Nights Dirty Lightning')
assert_no_difference('Ship.count') do
@pirate.update_attributes(:update_only_ship_attributes => { :name => 'Mayflower', :id => @ship.id })
end
assert_equal 'Mayflower', @ship.reload.name
assert_equal @ship, @pirate.reload.ship
end
def test_should_destroy_existing_when_update_only_is_true_and_id_is_given_and_is_marked_for_destruction
@ -284,9 +290,11 @@ def test_should_destroy_existing_when_update_only_is_true_and_id_is_given_and_is
@ship.delete
@ship = @pirate.create_update_only_ship(:name => 'Nights Dirty Lightning')
assert_difference('Ship.count', -1) do
@pirate.update_attributes(:update_only_ship_attributes => { :name => 'Mayflower', :id => @ship.id, :_destroy => true })
end
assert_nil @pirate.reload.ship
assert_raise(ActiveRecord::RecordNotFound) { Ship.find(@ship.id) }
Pirate.accepts_nested_attributes_for :update_only_ship, :update_only => true, :allow_destroy => false
end
@ -375,27 +383,24 @@ def test_should_modify_an_existing_record_if_there_is_a_matching_composite_id
def test_should_destroy_an_existing_record_if_there_is_a_matching_id_and_destroy_is_truthy
@ship.pirate.destroy
[1, '1', true, 'true'].each do |truth|
@ship.reload.create_pirate(:catchphrase => 'Arr')
assert_difference('Pirate.count', -1) do
@ship.update_attributes(:pirate_attributes => { :id => @ship.pirate.id, :_destroy => truth })
end
pirate = @ship.reload.create_pirate(:catchphrase => 'Arr')
@ship.update_attributes(:pirate_attributes => { :id => pirate.id, :_destroy => truth })
assert_raise(ActiveRecord::RecordNotFound) { pirate.reload }
end
end
def test_should_not_destroy_an_existing_record_if_destroy_is_not_truthy
[nil, '0', 0, 'false', false].each do |not_truth|
assert_no_difference('Pirate.count') do
@ship.update_attributes(:pirate_attributes => { :id => @ship.pirate.id, :_destroy => not_truth })
end
assert_nothing_raised(ActiveRecord::RecordNotFound) { @ship.pirate.reload }
end
end
def test_should_not_destroy_an_existing_record_if_allow_destroy_is_false
Ship.accepts_nested_attributes_for :pirate, :allow_destroy => false, :reject_if => proc { |attributes| attributes.empty? }
assert_no_difference('Pirate.count') do
@ship.update_attributes(:pirate_attributes => { :id => @ship.pirate.id, :_destroy => '1' })
end
assert_nothing_raised(ActiveRecord::RecordNotFound) { @ship.pirate.reload }
Ship.accepts_nested_attributes_for :pirate, :allow_destroy => true, :reject_if => proc { |attributes| attributes.empty? }
end
@ -409,10 +414,12 @@ def test_should_work_with_update_attributes_as_well
end
def test_should_not_destroy_the_associated_model_until_the_parent_is_saved
assert_no_difference('Pirate.count') do
@ship.attributes = { :pirate_attributes => { :id => @ship.pirate.id, '_destroy' => true } }
end
assert_difference('Pirate.count', -1) { @ship.save }
pirate = @ship.pirate
@ship.attributes = { :pirate_attributes => { :id => pirate.id, '_destroy' => true } }
assert_nothing_raised(ActiveRecord::RecordNotFound) { Pirate.find(pirate.id) }
@ship.save
assert_raise(ActiveRecord::RecordNotFound) { Pirate.find(pirate.id) }
end
def test_should_automatically_enable_autosave_on_the_association
@ -421,29 +428,28 @@ def test_should_automatically_enable_autosave_on_the_association
def test_should_create_new_model_when_nothing_is_there_and_update_only_is_true
@pirate.delete
assert_difference('Pirate.count', 1) do
@ship.reload.update_attributes(:update_only_pirate_attributes => { :catchphrase => 'Arr' })
end
@ship.reload.attributes = { :update_only_pirate_attributes => { :catchphrase => 'Arr' } }
assert @ship.update_only_pirate.new_record?
end
def test_should_update_existing_when_update_only_is_true_and_no_id_is_given
@pirate.delete
@pirate = @ship.create_update_only_pirate(:catchphrase => 'Aye')
assert_no_difference('Pirate.count') do
@ship.update_attributes(:update_only_pirate_attributes => { :catchphrase => 'Arr' })
end
assert_equal 'Arr', @pirate.reload.catchphrase
assert_equal @pirate, @ship.reload.update_only_pirate
end
def test_should_update_existing_when_update_only_is_true_and_id_is_given
@pirate.delete
@pirate = @ship.create_update_only_pirate(:catchphrase => 'Aye')
assert_no_difference('Pirate.count') do
@ship.update_attributes(:update_only_pirate_attributes => { :catchphrase => 'Arr', :id => @pirate.id })
end
assert_equal 'Arr', @pirate.reload.catchphrase
assert_equal @pirate, @ship.reload.update_only_pirate
end
def test_should_destroy_existing_when_update_only_is_true_and_id_is_given_and_is_marked_for_destruction
@ -451,9 +457,10 @@ def test_should_destroy_existing_when_update_only_is_true_and_id_is_given_and_is
@pirate.delete
@pirate = @ship.create_update_only_pirate(:catchphrase => 'Aye')
assert_difference('Pirate.count', -1) do
@ship.update_attributes(:update_only_pirate_attributes => { :catchphrase => 'Arr', :id => @pirate.id, :_destroy => true })
end
assert_raise(ActiveRecord::RecordNotFound) { @pirate.reload }
Ship.accepts_nested_attributes_for :update_only_pirate, :update_only => true, :allow_destroy => false
end
end