Skip to content
Permalink
Browse files

Add *_pair methods to create only two records

This introduces a set of methods for each build strategy where only two
records are created. Because the *_list methods can create an arbitrary
number (often too high), this introduces *_pair to ensure only two
records are created (and the number 2 doesn't need to be specified in
the call).
  • Loading branch information...
Andy Waite and Josh Clayton authored and joshuaclayton committed Oct 25, 2013
1 parent 7fd7121 commit b9e1dde7e8bb4497a711487641e751a310fdf996
@@ -868,6 +868,13 @@ To set the attributes for each of the factories, you can pass in a hash as you n
twenty_year_olds = FactoryGirl.build_list(:user, 25, date_of_birth: 20.years.ago)
```

There's also a set of `*_pair` methods for creating two records at a time:

```ruby
built_users = FactoryGirl.build_pair(:user) # array of two built users
created_users = FactoryGirl.create_pair(:user) # array of two created users
```

Custom Construction
-------------------

@@ -8,6 +8,7 @@ def initialize(strategy_name)
def define_strategy_methods
define_singular_strategy_method
define_list_strategy_method
define_pair_strategy_method
end

private
@@ -28,6 +29,14 @@ def define_list_strategy_method
end
end

def define_pair_strategy_method
strategy_name = @strategy_name

define_syntax_method("#{strategy_name}_pair") do |name, *traits_and_overrides, &block|
2.times.map { send(strategy_name, name, *traits_and_overrides, &block) }
end
end

def define_syntax_method(name, &block)
FactoryGirl::Syntax::Methods.module_exec do
define_method(name, &block)
@@ -0,0 +1,32 @@
require 'spec_helper'

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

FactoryGirl.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 { FactoryGirl.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
@@ -49,6 +49,14 @@ def result(evaluation)
expect(inserted_items.length).to eq 2
expect(inserted_items.map(&:name)).to eq ["Custom strategy", "Custom strategy"]
end

it "allows using the *_pair method to build a list using a custom strategy" do
FactoryGirl.register_strategy(:insert, custom_strategy)

inserted_items = FactoryGirl.insert_pair(:named_object)
expect(inserted_items.length).to eq 2
expect(inserted_items.map(&:name)).to eq ["Custom strategy", "Custom strategy"]
end
end

describe "including FactoryGirl::Syntax::Methods when custom strategies have been declared" do

0 comments on commit b9e1dde

Please sign in to comment.
You can’t perform that action at this time.
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session.