Testing FactoryBot Create Pair Implementation in factory_bot
This test suite examines FactoryBot’s create_pair functionality for generating multiple test instances simultaneously. The tests verify the creation of Post model instances with predefined attributes and validate both object persistence and default value assignment.
Test Coverage Overview
Implementation Analysis
Technical Details
Best Practices Demonstrated
thoughtbot/factory_bot
spec/acceptance/create_pair_spec.rb
describe "create multiple instances" do
before do
define_model("Post", title: :string, position: :integer)
FactoryBot.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 { FactoryBot.create_pair(:post) }
its(:length) { should eq 2 }
it "creates all the posts" do
subject.each do |record|
expect(record).not_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
end