diff --git a/lib/factory_bot/evaluator.rb b/lib/factory_bot/evaluator.rb index e2628a4ff..70e7ef284 100644 --- a/lib/factory_bot/evaluator.rb +++ b/lib/factory_bot/evaluator.rb @@ -24,7 +24,7 @@ def initialize(build_strategy, overrides = {}) def association(factory_name, *traits_and_overrides) overrides = traits_and_overrides.extract_options! strategy_override = overrides.fetch(:strategy) { - FactoryBot.use_parent_strategy ? @build_strategy.class : :create + FactoryBot.use_parent_strategy ? @build_strategy.to_sym : :create } traits_and_overrides += [overrides.except(:strategy)] diff --git a/lib/factory_bot/strategy/attributes_for.rb b/lib/factory_bot/strategy/attributes_for.rb index c4273a0c6..9b0b8df94 100644 --- a/lib/factory_bot/strategy/attributes_for.rb +++ b/lib/factory_bot/strategy/attributes_for.rb @@ -8,6 +8,10 @@ def association(runner) def result(evaluation) evaluation.hash end + + def to_sym + :attributes_for + end end end end diff --git a/lib/factory_bot/strategy/build.rb b/lib/factory_bot/strategy/build.rb index e65285227..d18bb8ff4 100644 --- a/lib/factory_bot/strategy/build.rb +++ b/lib/factory_bot/strategy/build.rb @@ -10,6 +10,10 @@ def result(evaluation) evaluation.notify(:after_build, instance) end end + + def to_sym + :build + end end end end diff --git a/lib/factory_bot/strategy/create.rb b/lib/factory_bot/strategy/create.rb index 1e66e58cc..c9371f42d 100644 --- a/lib/factory_bot/strategy/create.rb +++ b/lib/factory_bot/strategy/create.rb @@ -13,6 +13,10 @@ def result(evaluation) evaluation.notify(:after_create, instance) end end + + def to_sym + :create + end end end end diff --git a/lib/factory_bot/strategy/null.rb b/lib/factory_bot/strategy/null.rb index 893826f58..758b031e0 100644 --- a/lib/factory_bot/strategy/null.rb +++ b/lib/factory_bot/strategy/null.rb @@ -6,6 +6,10 @@ def association(runner) def result(evaluation) end + + def to_sym + :null + end end end end diff --git a/lib/factory_bot/strategy/stub.rb b/lib/factory_bot/strategy/stub.rb index 8ff1ff452..a2f9aef90 100644 --- a/lib/factory_bot/strategy/stub.rb +++ b/lib/factory_bot/strategy/stub.rb @@ -41,6 +41,10 @@ def result(evaluation) end end + def to_sym + :stub + end + private def next_id diff --git a/spec/acceptance/activesupport_instrumentation_spec.rb b/spec/acceptance/activesupport_instrumentation_spec.rb index 0f133d143..6ad068720 100644 --- a/spec/acceptance/activesupport_instrumentation_spec.rb +++ b/spec/acceptance/activesupport_instrumentation_spec.rb @@ -16,6 +16,10 @@ def subscribed(callback, *args) let(:user_factory) { FactoryBot::Internal.factory_by_name("user") } before do define_model("User", email: :string) + define_model("Post", user_id: :integer) do + belongs_to :user + end + FactoryBot.define do factory :user do email { "john@example.com" } @@ -24,6 +28,12 @@ def subscribed(callback, *args) after(:build) { Kernel.sleep(0.1) } end end + + factory :post do + trait :with_user do + user + end + end end end @@ -55,6 +65,9 @@ def subscribed(callback, *args) FactoryBot.build_list(:user, 5) FactoryBot.create_list(:user, 2) FactoryBot.attributes_for(:slow_user) + user = FactoryBot.create(:user) + FactoryBot.create(:post, user: user) + FactoryBot.create_list(:post, 2, :with_user) end expect(tracked_invocations[:slow_user][:build]).to eq(2) @@ -62,5 +75,6 @@ def subscribed(callback, *args) expect(tracked_invocations[:slow_user][:factory]).to eq(slow_user_factory) expect(tracked_invocations[:user][:build]).to eq(5) expect(tracked_invocations[:user][:factory]).to eq(user_factory) + expect(tracked_invocations[:user][:create]).to eq(5) end end