Sha256: 445d7befe0fb9767e94de86d5ef975631db09f3833c017f7802ab8b9457e015c

Contents?: true

Size: 895 Bytes

Versions: 10

Compression:

Stored size: 895 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

10 entries across 7 versions & 4 rubygems

Version Path
sc_core-0.0.7 test/dummy/vendor/bundle/ruby/2.2.0/gems/factory_girl-4.5.0/spec/acceptance/nested_attributes_spec.rb
solidus_backend-1.0.0.pre3 vendor/bundle/gems/factory_girl-4.5.0/spec/acceptance/nested_attributes_spec.rb
solidus_backend-1.0.0.pre2 vendor/bundle/gems/factory_girl-4.5.0/spec/acceptance/nested_attributes_spec.rb
solidus_backend-1.0.0.pre vendor/bundle/gems/factory_girl-4.5.0/spec/acceptance/nested_attributes_spec.rb
apl-library-0.0.90 vendor/bundle/ruby/2.1.0/gems/factory_girl-4.4.0/spec/acceptance/nested_attributes_spec.rb
apl-library-0.0.90 vendor/bundle/ruby/1.9.1/gems/factory_girl-4.4.0/spec/acceptance/nested_attributes_spec.rb
apl-library-0.0.90 vendor/bundle/ruby/2.1.0/gems/apl-library-0.0.90/vendor/bundle/ruby/1.9.1/gems/factory_girl-4.4.0/spec/acceptance/nested_attributes_spec.rb
apl-library-0.0.90 vendor/bundle/ruby/2.1.0/gems/apl-library-0.0.90/vendor/bundle/ruby/2.1.0/gems/factory_girl-4.4.0/spec/acceptance/nested_attributes_spec.rb
factory_girl-4.5.0 spec/acceptance/nested_attributes_spec.rb
factory_girl-4.4.0 spec/acceptance/nested_attributes_spec.rb