Skip to content

Commit

Permalink
Merge pull request #41 from rubocop/document-active_model-hardcoded-t…
Browse files Browse the repository at this point in the history
…ransient-assiciation-case

Add an explanation to #39
  • Loading branch information
ydah authored May 15, 2023
2 parents 4436d57 + 3b10d2a commit 1ea20dc
Showing 1 changed file with 21 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -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)
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 1ea20dc

Please sign in to comment.