2011-01-04 16:12:04 -05:00
|
|
|
describe "build multiple instances" do
|
|
|
|
before do
|
2018-10-07 21:45:51 -04:00
|
|
|
define_model("Post", title: :string, position: :integer)
|
2011-01-04 16:12:04 -05:00
|
|
|
|
2017-10-20 15:20:28 -04:00
|
|
|
FactoryBot.define do
|
2011-01-04 16:12:04 -05:00
|
|
|
factory(:post) do |post|
|
2018-07-29 11:30:02 -04:00
|
|
|
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
|
2017-10-20 15:20:28 -04:00
|
|
|
subject { FactoryBot.build_list(:post, 20) }
|
2011-01-04 16:12:04 -05:00
|
|
|
|
2013-01-18 13:27:57 -05:00
|
|
|
its(:length) { should eq 20 }
|
2011-01-04 16:12:04 -05:00
|
|
|
|
|
|
|
it "builds (but doesn't save) all the posts" do
|
|
|
|
subject.each do |record|
|
2013-01-18 13:27:57 -05:00
|
|
|
expect(record).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
|
2017-10-20 15:20:28 -04:00
|
|
|
subject { FactoryBot.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|
|
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
|
2017-10-20 15:20:28 -04:00
|
|
|
FactoryBot.build_list(:post, 20, title: "The Listing of the Block") do |post|
|
2012-10-19 14:24:08 -04:00
|
|
|
post.position = post.id
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
it "correctly uses the set value" do
|
2018-10-05 14:54:08 -04:00
|
|
|
subject.each do |record|
|
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
|
2020-04-24 11:36:17 -04:00
|
|
|
|
|
|
|
context "with a block that receives both the object and an index" do
|
|
|
|
subject do
|
|
|
|
FactoryBot.build_list(:post, 20, title: "The Indexed Block") do |post, index|
|
|
|
|
post.position = index
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
it "correctly uses the set value" do
|
|
|
|
subject.each_with_index do |record, index|
|
|
|
|
expect(record.position).to eq index
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2011-01-04 16:12:04 -05:00
|
|
|
end
|