diff --git a/spec/rubocop/cop/factory_bot/factory_association_with_strategy_spec.rb b/spec/rubocop/cop/factory_bot/factory_association_with_strategy_spec.rb index ccc84a15..50d6e479 100644 --- a/spec/rubocop/cop/factory_bot/factory_association_with_strategy_spec.rb +++ b/spec/rubocop/cop/factory_bot/factory_association_with_strategy_spec.rb @@ -1,7 +1,7 @@ # frozen_string_literal: true RSpec.describe RuboCop::Cop::FactoryBot::FactoryAssociationWithStrategy do - context 'when passing an hardcoded strategy' do + context 'when passing a hardcoded strategy' do context 'when passing a `create` strategy' do it 'flags the strategy' do expect_offense(<<~RUBY) @@ -59,6 +59,26 @@ RUBY end end + + context 'when inside a transient block' do + # Using an association inside of the `transient` block is not supported, + # as it would initialize the association as if it was outside of the + # `transient` block. But if the referenced factory is backed by + # `ActiveModel::Model` and declares `skip_create`, it can be used. + # Otherwise, there is usually a better way than building a model + # instance that is not directly referenced. + it 'flags the strategy' do + expect_offense(<<~RUBY) + factory :foo do + transient do + profile { create(:profile, :qualified) } + ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Use an implicit, explicit or inline definition instead of hard coding a strategy for setting association within factory. + account { association(:fiscal_year) } # No offense + end + end + RUBY + end + end end context 'when passing a block who does not use strategy' do