2011-01-04 16:12:04 -05:00
|
|
|
require 'spec_helper'
|
|
|
|
|
|
|
|
describe "create multiple instances" do
|
|
|
|
before do
|
2012-10-19 14:24:08 -04:00
|
|
|
define_model('Post', title: :string, position: :integer)
|
2011-01-04 16:12:04 -05:00
|
|
|
|
|
|
|
FactoryGirl.define do
|
|
|
|
factory(:post) do |post|
|
|
|
|
post.title "Through the Looking Glass"
|
2012-10-19 14:24:08 -04:00
|
|
|
post.position { rand(10**4) }
|
2011-01-04 16:12:04 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context "without default attributes" do
|
|
|
|
subject { FactoryGirl.create_list(:post, 20) }
|
|
|
|
|
2013-01-18 13:27:57 -05:00
|
|
|
its(:length) { should eq 20 }
|
2011-01-04 16:12:04 -05:00
|
|
|
|
|
|
|
it "creates all the posts" do
|
|
|
|
subject.each do |record|
|
2013-01-18 13:27:57 -05:00
|
|
|
expect(record).not_to be_new_record
|
2011-01-04 16:12:04 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
it "uses the default factory values" do
|
|
|
|
subject.each do |record|
|
2013-01-18 13:27:57 -05:00
|
|
|
expect(record.title).to eq "Through the Looking Glass"
|
2011-01-04 16:12:04 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context "with default attributes" do
|
2012-03-09 17:20:38 -05:00
|
|
|
subject { FactoryGirl.create_list(:post, 20, title: "The Hunting of the Snark") }
|
2011-01-04 16:12:04 -05:00
|
|
|
|
|
|
|
it "overrides the default values" do
|
|
|
|
subject.each do |record|
|
2013-01-18 13:27:57 -05:00
|
|
|
expect(record.title).to eq "The Hunting of the Snark"
|
2011-01-04 16:12:04 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2012-10-19 14:24:08 -04:00
|
|
|
|
|
|
|
context "with a block" do
|
|
|
|
subject do
|
|
|
|
FactoryGirl.create_list(:post, 20, title: "The Listing of the Block") do |post|
|
|
|
|
post.position = post.id
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
it "uses the new values" do
|
|
|
|
subject.each_with_index do |record, index|
|
2013-01-18 13:27:57 -05:00
|
|
|
expect(record.position).to eq record.id
|
2012-10-19 14:24:08 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2016-08-26 08:40:56 -04:00
|
|
|
|
|
|
|
context "without the count" do
|
|
|
|
subject { FactoryGirl.create_list(:post, title: "The Hunting of the Bear") }
|
|
|
|
|
|
|
|
it "raise ArgumentError with the proper error message" do
|
|
|
|
expect { subject }.to raise_error(ArgumentError, /count missing/)
|
|
|
|
end
|
|
|
|
end
|
2011-01-04 16:12:04 -05:00
|
|
|
end
|
2012-02-17 15:20:46 -05:00
|
|
|
|
2014-04-28 17:39:44 -04:00
|
|
|
describe "multiple creates and transient attributes to dynamically build attribute lists" do
|
2012-02-17 15:20:46 -05:00
|
|
|
before do
|
2012-03-09 17:20:38 -05:00
|
|
|
define_model('User', name: :string) do
|
2012-02-17 15:20:46 -05:00
|
|
|
has_many :posts
|
|
|
|
end
|
|
|
|
|
2012-03-09 17:20:38 -05:00
|
|
|
define_model('Post', title: :string, user_id: :integer) do
|
2012-02-17 15:20:46 -05:00
|
|
|
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
|
2014-04-28 17:39:44 -04:00
|
|
|
transient do
|
2012-02-17 15:20:46 -05:00
|
|
|
posts_count 5
|
|
|
|
end
|
|
|
|
|
2012-05-04 16:48:46 -04:00
|
|
|
after(:create) do |user, evaluator|
|
2012-03-09 17:20:38 -05:00
|
|
|
FactoryGirl.create_list(:post, evaluator.posts_count, user: user)
|
2012-02-17 15:20:46 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
it "generates the correct number of posts" do
|
2013-01-18 13:27:57 -05:00
|
|
|
expect(FactoryGirl.create(:user_with_posts).posts.length).to eq 5
|
2012-02-17 15:20:46 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it "allows the number of posts to be modified" do
|
2013-01-18 13:27:57 -05:00
|
|
|
expect(FactoryGirl.create(:user_with_posts, posts_count: 2).posts.length).to eq 2
|
2012-02-17 15:20:46 -05:00
|
|
|
end
|
|
|
|
end
|