2012-03-16 13:04:31 -04:00
|
|
|
describe "association assignment from nested attributes" do
|
|
|
|
before do
|
2012-03-09 17:20:38 -05:00
|
|
|
define_model("Post", title: :string) do
|
2012-03-16 13:04:31 -04:00
|
|
|
has_many :comments
|
|
|
|
accepts_nested_attributes_for :comments
|
|
|
|
end
|
|
|
|
|
2012-03-09 17:20:38 -05:00
|
|
|
define_model("Comment", post_id: :integer, body: :text) do
|
2012-03-16 13:04:31 -04:00
|
|
|
belongs_to :post
|
|
|
|
end
|
|
|
|
|
2017-10-20 15:20:28 -04:00
|
|
|
FactoryBot.define do
|
2012-03-16 13:04:31 -04:00
|
|
|
factory :post do
|
2017-10-20 15:20:28 -04:00
|
|
|
comments_attributes { [FactoryBot.attributes_for(:comment), FactoryBot.attributes_for(:comment)] }
|
2012-03-16 13:04:31 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
factory :comment do
|
2013-12-14 22:33:15 -05:00
|
|
|
sequence(:body) { |n| "Body #{n}" }
|
2012-03-16 13:04:31 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
it "assigns the correct amount of comments" do
|
2017-10-20 15:20:28 -04:00
|
|
|
expect(FactoryBot.create(:post).comments.count).to eq 2
|
2012-03-16 13:04:31 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it "assigns the correct amount of comments when overridden" do
|
2018-10-21 11:39:48 -04:00
|
|
|
post = FactoryBot.create(:post, comments_attributes: [FactoryBot.attributes_for(:comment)])
|
|
|
|
|
|
|
|
expect(post.comments.count).to eq 1
|
2012-03-16 13:04:31 -04:00
|
|
|
end
|
|
|
|
end
|