Sha256: 0702472090ddc319945b057c7e1cd2d2a69e46dfc577c8bc5f96924a684072a0

Contents?: true

Size: 1.87 KB

Versions: 88

Compression:

Stored size: 1.87 KB

Contents

require 'spec_helper'

describe "create multiple instances" do
  before do
    define_model('Post', title: :string)

    FactoryGirl.define do
      factory(:post) do |post|
        post.title "Through the Looking Glass"
      end
    end
  end

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

    its(:length) { should == 20 }

    it "creates all the posts" do
      subject.each do |record|
        record.should_not be_new_record
      end
    end

    it "uses the default factory values" do
      subject.each do |record|
        record.title.should == "Through the Looking Glass"
      end
    end
  end

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

    it "overrides the default values" do
      subject.each do |record|
        record.title.should == "The Hunting of the Snark"
      end
    end
  end
end

describe "multiple creates and ignored attributes to dynamically build attribute lists" do
  before do
    define_model('User', name: :string) do
      has_many :posts
    end

    define_model('Post', title: :string, user_id: :integer) do
      belongs_to :user
    end

    FactoryGirl.define do
      factory :post do
        title "Through the Looking Glass"
        user
      end

      factory :user do
        name "John Doe"

        factory :user_with_posts do
          ignore do
            posts_count 5
          end

          after(:create) do |user, evaluator|
            FactoryGirl.create_list(:post, evaluator.posts_count, user: user)
          end
        end
      end
    end
  end

  it "generates the correct number of posts" do
    FactoryGirl.create(:user_with_posts).posts.length.should == 5
  end

  it "allows the number of posts to be modified" do
    FactoryGirl.create(:user_with_posts, posts_count: 2).posts.length.should == 2
  end
end

Version data entries

88 entries across 79 versions & 5 rubygems

Version Path
classiccms-0.7.5 vendor/bundle/gems/factory_girl-3.3.0/spec/acceptance/create_list_spec.rb
classiccms-0.7.4 vendor/bundle/gems/factory_girl-3.3.0/spec/acceptance/create_list_spec.rb
classiccms-0.7.3 vendor/bundle/gems/factory_girl-3.3.0/spec/acceptance/create_list_spec.rb
classiccms-0.7.2 vendor/bundle/gems/factory_girl-3.3.0/spec/acceptance/create_list_spec.rb
classiccms-0.7.1 vendor/bundle/gems/factory_girl-3.3.0/spec/acceptance/create_list_spec.rb
classiccms-0.7.0 vendor/bundle/gems/factory_girl-3.3.0/spec/acceptance/create_list_spec.rb
challah-1.0.0.beta vendor/bundle/gems/factory_girl-4.1.0/spec/acceptance/create_list_spec.rb
challah-0.9.1.beta.3 vendor/bundle/gems/factory_girl-4.1.0/spec/acceptance/create_list_spec.rb
challah-0.9.1.beta vendor/bundle/gems/factory_girl-4.1.0/spec/acceptance/create_list_spec.rb
sunrise-cms-0.5.0.rc1 vendor/bundle/ruby/1.9.1/gems/factory_girl-4.1.0/spec/acceptance/create_list_spec.rb
challah-0.9.0 vendor/bundle/gems/factory_girl-4.1.0/spec/acceptance/create_list_spec.rb
classiccms-0.6.9 vendor/bundle/gems/factory_girl-3.3.0/spec/acceptance/create_list_spec.rb
challah-rolls-0.2.0 vendor/bundle/gems/challah-0.8.3/vendor/bundle/gems/factory_girl-4.0.0/spec/acceptance/create_list_spec.rb
challah-rolls-0.2.0 vendor/bundle/gems/factory_girl-4.1.0/spec/acceptance/create_list_spec.rb
challah-rolls-0.2.0 vendor/bundle/gems/challah-0.8.3/vendor/bundle/gems/factory_girl-4.1.0/spec/acceptance/create_list_spec.rb
challah-rolls-0.2.0 vendor/bundle/gems/factory_girl-3.6.1/spec/acceptance/create_list_spec.rb
challah-rolls-0.2.0 vendor/bundle/gems/challah-0.8.0.pre/vendor/bundle/gems/factory_girl-3.6.1/spec/acceptance/create_list_spec.rb
challah-rolls-0.2.0 vendor/bundle/gems/factory_girl-4.0.0/spec/acceptance/create_list_spec.rb
challah-0.8.3 vendor/bundle/gems/factory_girl-4.0.0/spec/acceptance/create_list_spec.rb
challah-0.8.3 vendor/bundle/gems/factory_girl-4.1.0/spec/acceptance/create_list_spec.rb