Sha256: 5b5b65e0868a790b09ee7aad2dcc0f85cc4ec8b48d514e36315dd60b2c8b52f2

Contents?: true

Size: 894 Bytes

Versions: 6

Compression:

Stored size: 894 Bytes

Contents

require "spec_helper"

describe "association assignment from nested attributes" do
  before do
    define_model("Post", title: :string) do
      has_many :comments
      accepts_nested_attributes_for :comments
    end

    define_model("Comment", post_id: :integer, body: :text) do
      belongs_to :post
    end

    FactoryGirl.define do
      factory :post do
        comments_attributes { [FactoryGirl.attributes_for(:comment), FactoryGirl.attributes_for(:comment)] }
      end

      factory :comment do
        sequence(:body) {|n| "Body #{n}" }
      end
    end
  end

  it "assigns the correct amount of comments" do
    expect(FactoryGirl.create(:post).comments.count).to eq 2
  end

  it "assigns the correct amount of comments when overridden" do
    expect(FactoryGirl.create(:post, :comments_attributes => [FactoryGirl.attributes_for(:comment)]).comments.count).to eq 1
  end
end

Version data entries

6 entries across 6 versions & 2 rubygems

Version Path
factory_girl-4.3.0 spec/acceptance/nested_attributes_spec.rb
challah-1.0.0 vendor/bundle/gems/factory_girl-4.2.0/spec/acceptance/nested_attributes_spec.rb
challah-1.0.0.beta3 vendor/bundle/gems/factory_girl-4.2.0/spec/acceptance/nested_attributes_spec.rb
challah-1.0.0.beta2 vendor/bundle/gems/factory_girl-4.2.0/spec/acceptance/nested_attributes_spec.rb
challah-1.0.0.beta vendor/bundle/gems/factory_girl-4.2.0/spec/acceptance/nested_attributes_spec.rb
factory_girl-4.2.0 spec/acceptance/nested_attributes_spec.rb