1
0
Fork 0
mirror of https://github.com/thoughtbot/factory_bot.git synced 2022-11-09 11:43:51 -05:00

Add use_parent_strategy option for building associations (#961)

This change means that:

1. The option is turned on, and
2. A Post has a User (for example), and
3. We build an association
4. Then the User is built too.

With the flag off, the User would be created, which matches current
behaviour.

See: https://github.com/thoughtbot/factory_girl/pull/749
This commit is contained in:
Ryan Ringler 2016-12-16 02:28:09 -08:00 committed by Josh Clayton
parent e369187a0d
commit 69b72e66de
6 changed files with 72 additions and 17 deletions

View file

@ -68,10 +68,23 @@ module FactoryGirl
end
class << self
delegate :factories, :sequences, :traits, :callbacks, :strategies, :callback_names,
:to_create, :skip_create, :initialize_with, :constructor,
:duplicate_attribute_assignment_from_initialize_with, :duplicate_attribute_assignment_from_initialize_with=,
:allow_class_lookup, :allow_class_lookup=, to: :configuration
delegate :factories,
:sequences,
:traits,
:callbacks,
:strategies,
:callback_names,
:to_create,
:skip_create,
:initialize_with,
:constructor,
:duplicate_attribute_assignment_from_initialize_with,
:duplicate_attribute_assignment_from_initialize_with=,
:allow_class_lookup,
:allow_class_lookup=,
:use_parent_strategy,
:use_parent_strategy=,
to: :configuration
end
def self.register_factory(factory)

View file

@ -3,7 +3,7 @@ module FactoryGirl
class Configuration
attr_reader :factories, :sequences, :traits, :strategies, :callback_names
attr_accessor :allow_class_lookup
attr_accessor :allow_class_lookup, :use_parent_strategy
def initialize
@factories = Decorator::DisallowsDuplicatesRegistry.new(Registry.new('Factory'))

View file

@ -23,7 +23,9 @@ module FactoryGirl
def association(factory_name, *traits_and_overrides)
overrides = traits_and_overrides.extract_options!
strategy_override = overrides.fetch(:strategy) { :create }
strategy_override = overrides.fetch(:strategy) do
FactoryGirl.use_parent_strategy ? @build_strategy.class : :create
end
traits_and_overrides += [overrides.except(:strategy)]

View file

@ -23,13 +23,33 @@ describe "a built instance" do
it { should be_new_record }
it "assigns and saves associations" do
context "when the :use_parent_strategy config option has not been set" do
before { FactoryGirl.use_parent_strategy = nil }
it "assigns and saves associations" do
expect(subject.user).to be_kind_of(User)
expect(subject.user).not_to be_new_record
end
end
context "when the :use_parent_strategy config option has been enabled" do
before { FactoryGirl.use_parent_strategy = true }
it "assigns but does not save associations" do
expect(subject.user).to be_kind_of(User)
expect(subject.user).to be_new_record
end
end
it "assigns but does not save associations when using parent strategy" do
FactoryGirl.use_parent_strategy = true
expect(subject.user).to be_kind_of(User)
expect(subject.user).not_to be_new_record
expect(subject.user).to be_new_record
end
end
describe "a built instance with strategy: :build" do
describe "a built instance with strategy: :create" do
include FactoryGirl::Syntax::Methods
before do
@ -43,7 +63,7 @@ describe "a built instance with strategy: :build" do
factory :user
factory :post do
association(:user, strategy: :build)
association(:user, strategy: :create)
end
end
end
@ -52,9 +72,9 @@ describe "a built instance with strategy: :build" do
it { should be_new_record }
it "assigns but does not save associations" do
it "assigns and saves associations" do
expect(subject.user).to be_kind_of(User)
expect(subject.user).to be_new_record
expect(subject.user).not_to be_new_record
end
end

View file

@ -35,7 +35,7 @@ describe "a generated stub instance" do
end
it "assigns associations" do
expect(subject.user).not_to be_nil
expect(subject.user).to be_kind_of(User)
end
it "has an id" do
@ -51,6 +51,10 @@ describe "a generated stub instance" do
expect(subject).not_to be_new_record
end
it "assigns associations that aren't new records" do
expect(subject.user).not_to be_new_record
end
it "isn't changed" do
expect(subject).not_to be_changed
end

View file

@ -100,10 +100,26 @@ describe "associations without overriding :strategy" do
end
end
it "uses the overridden create strategy to create the association" do
FactoryGirl.register_strategy(:create, custom_strategy)
post = FactoryGirl.build(:post)
expect(post.user.name).to eq "Custom strategy"
context "when the :use_parent_strategy config option has not been enabled" do
before { FactoryGirl.use_parent_strategy = nil }
it "uses the overridden strategy on the association" do
FactoryGirl.register_strategy(:create, custom_strategy)
post = FactoryGirl.build(:post)
expect(post.user.name).to eq "Custom strategy"
end
end
context "when the :use_parent_strategy config option has been enabled" do
before { FactoryGirl.use_parent_strategy = true }
it "uses the parent strategy on the association" do
FactoryGirl.register_strategy(:create, custom_strategy)
post = FactoryGirl.build(:post)
expect(post.user.name).to eq "John Doe"
end
end
end