Sha256: 34b335ce5c8a27cd3138b9f707eceede48f0c6a4460a83af38d643f07989913c

Contents?: true

Size: 1.33 KB

Versions: 16

Compression:

Stored size: 1.33 KB

Contents

require 'spec_helper'

describe "build multiple instances" do
  before do
    define_model('Post', title: :string, position: :integer)

    FactoryGirl.define do
      factory(:post) do |post|
        post.title "Through the Looking Glass"
        post.position { rand(10**4) }
      end
    end
  end

  context "without default attributes" do
    subject { FactoryGirl.build_list(:post, 20) }

    its(:length) { should eq 20 }

    it "builds (but doesn't save) all the posts" do
      subject.each do |record|
        expect(record).to be_new_record
      end
    end

    it "uses the default factory values" do
      subject.each do |record|
        expect(record.title).to eq "Through the Looking Glass"
      end
    end
  end

  context "with default attributes" do
    subject { FactoryGirl.build_list(:post, 20, title: "The Hunting of the Snark") }

    it "overrides the default values" do
      subject.each do |record|
        expect(record.title).to eq "The Hunting of the Snark"
      end
    end
  end

  context "with a block" do
    subject do
      FactoryGirl.build_list(:post, 20, title: "The Listing of the Block") do |post|
        post.position = post.id
      end
    end

    it "correctly uses the set value" do
      subject.each_with_index do |record, index|
        expect(record.position).to eq record.id
      end
    end
  end
end

Version data entries

16 entries across 13 versions & 5 rubygems

Version Path
sc_core-0.0.7 test/dummy/vendor/bundle/ruby/2.2.0/gems/factory_girl-4.5.0/spec/acceptance/build_list_spec.rb
solidus_backend-1.0.0.pre3 vendor/bundle/gems/factory_girl-4.5.0/spec/acceptance/build_list_spec.rb
solidus_backend-1.0.0.pre2 vendor/bundle/gems/factory_girl-4.5.0/spec/acceptance/build_list_spec.rb
solidus_backend-1.0.0.pre vendor/bundle/gems/factory_girl-4.5.0/spec/acceptance/build_list_spec.rb
apl-library-0.0.90 vendor/bundle/ruby/1.9.1/gems/factory_girl-4.4.0/spec/acceptance/build_list_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/build_list_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/build_list_spec.rb
apl-library-0.0.90 vendor/bundle/ruby/2.1.0/gems/factory_girl-4.4.0/spec/acceptance/build_list_spec.rb
factory_girl-4.5.0 spec/acceptance/build_list_spec.rb
factory_girl-4.4.0 spec/acceptance/build_list_spec.rb
factory_girl-4.3.0 spec/acceptance/build_list_spec.rb
challah-1.0.0 vendor/bundle/gems/factory_girl-4.2.0/spec/acceptance/build_list_spec.rb
challah-1.0.0.beta3 vendor/bundle/gems/factory_girl-4.2.0/spec/acceptance/build_list_spec.rb
challah-1.0.0.beta2 vendor/bundle/gems/factory_girl-4.2.0/spec/acceptance/build_list_spec.rb
challah-1.0.0.beta vendor/bundle/gems/factory_girl-4.2.0/spec/acceptance/build_list_spec.rb
factory_girl-4.2.0 spec/acceptance/build_list_spec.rb