Skip to content

Commit

Permalink
add test for nested attributes
Browse files Browse the repository at this point in the history
  • Loading branch information
msimonborg committed Jul 20, 2017
1 parent f05c1a4 commit 8efa4d9
Showing 1 changed file with 16 additions and 0 deletions.
16 changes: 16 additions & 0 deletions spec/lazy_record/collections_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,12 @@
Parent.class_eval do
lr_has_many :children
lr_has_many :brothers, class_name: 'Sibling'
lr_accepts_nested_attributes_for :children, :brothers
end

Child.class_eval { attr_accessor :age }
Sibling.class_eval { attr_accessor :name }

it 'has many children' do
expect(Parent.new).to respond_to(:children)
end
Expand Down Expand Up @@ -72,5 +76,17 @@
expect(&add_non_siblings).to raise_error(ArgumentError, 'Argument must be a collection of siblings')
expect(&add_non_sibling).to raise_error(NoMethodError)
end

it 'accepts nested attributes' do
parent = Parent.new

parent.children_attributes = { 0 => { age: 11 }, 1 => { age: 12 } }
parent.brothers_attributes = { 0 => { name: 'Sue' }, 1 => { name: 'Bob' } }

expect(parent.children_count).to eq 2
expect(parent.brothers_count).to eq 2
expect(parent.children.map(&:age)).to eq [11, 12]
expect(parent.brothers.map(&:name)).to eq %w[Sue Bob]
end
end
end

0 comments on commit 8efa4d9

Please sign in to comment.