2012-03-16 13:04:31 -04:00
|
|
|
require "spec_helper"
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
FactoryGirl.define do
|
|
|
|
factory :post do
|
|
|
|
comments_attributes { [FactoryGirl.attributes_for(:comment), FactoryGirl.attributes_for(:comment)] }
|
|
|
|
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
|
2013-01-18 13:27:57 -05:00
|
|
|
expect(FactoryGirl.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
|
2013-01-18 13:27:57 -05:00
|
|
|
expect(FactoryGirl.create(:post, :comments_attributes => [FactoryGirl.attributes_for(:comment)]).comments.count).to eq 1
|
2012-03-16 13:04:31 -04:00
|
|
|
end
|
|
|
|
end
|