1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

Add test case for autosave HasMany with accepts_nested_attributes.

It should not save the parent record when the nested attributes are
invalid.

Test case to cover #8194.
This commit is contained in:
Lauro Caetano 2014-02-08 18:46:32 -02:00
parent 77577149f7
commit 1ea61cb9bc
3 changed files with 33 additions and 0 deletions

View file

@ -17,6 +17,8 @@ require 'models/tag'
require 'models/tagging'
require 'models/treasure'
require 'models/eye'
require 'models/electron'
require 'models/molecule'
class TestAutosaveAssociationsInGeneral < ActiveRecord::TestCase
def test_should_not_add_the_same_callbacks_multiple_times_for_has_one
@ -343,6 +345,33 @@ class TestDefaultAutosaveAssociationOnABelongsToAssociation < ActiveRecord::Test
end
end
class TestDefaultAutosaveAssociationOnAHasManyAssociationWithAcceptsNestedAttributes < ActiveRecord::TestCase
def test_invalid_adding_with_nested_attributes
molecule = Molecule.new
valid_electron = Electron.new(name: 'electron')
invalid_electron = Electron.new
molecule.electrons = [valid_electron, invalid_electron]
molecule.save
assert_not invalid_electron.valid?
assert valid_electron.valid?
assert_not molecule.persisted?, 'Molecule should not be persisted when its electrons are invalid'
end
def test_valid_adding_with_nested_attributes
molecule = Molecule.new
valid_electron = Electron.new(name: 'electron')
molecule.electrons = [valid_electron]
molecule.save
assert valid_electron.valid?
assert molecule.persisted?
assert_equal 1, molecule.electrons.count
end
end
class TestDefaultAutosaveAssociationOnAHasManyAssociation < ActiveRecord::TestCase
fixtures :companies, :people

View file

@ -1,3 +1,5 @@
class Electron < ActiveRecord::Base
belongs_to :molecule
validates_presence_of :name
end

View file

@ -1,4 +1,6 @@
class Molecule < ActiveRecord::Base
belongs_to :liquid
has_many :electrons
accepts_nested_attributes_for :electrons
end