2011-01-04 16:12:04 -05:00
|
|
|
require 'spec_helper'
|
|
|
|
|
|
|
|
describe "build 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.build_list(:post, 20) }
|
|
|
|
|
|
|
|
its(:length) { should == 20 }
|
|
|
|
|
|
|
|
it "builds (but doesn't save) all the posts" do
|
|
|
|
subject.each do |record|
|
|
|
|
record.should 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
|
2012-03-09 17:20:38 -05:00
|
|
|
subject { FactoryGirl.build_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|
|
|
|
|
record.title.should == "The Hunting of the Snark"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2012-10-19 14:24:08 -04:00
|
|
|
|
|
|
|
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|
|
|
|
|
record.position.should == record.id
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2011-01-04 16:12:04 -05:00
|
|
|
end
|