Skip to content

Commit

Permalink
Merge pull request #38 from rubocop/fix-association-style-error
Browse files Browse the repository at this point in the history
Fix AssociationStyle for blockless factory definitions
  • Loading branch information
pirj authored May 15, 2023
2 parents f334eb6 + 0927b5c commit 4436d57
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 5 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

## Master (Unreleased)

- Fix `FactoryBot/AssociationStyle` cop for a blockless `factory`. ([@pirj])

## 2.23.0 (2023-05-15)

- Add `FactoryBot/FactoryAssociationWithStrategy` cop. ([@morissetcl])
Expand Down
2 changes: 1 addition & 1 deletion lib/rubocop/cop/factory_bot/association_style.rb
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ def bad_associations_in(node)
end

def children_of_factory_block(node)
block = node.parent
block = node.block_node
return [] unless block
return [] unless block.body

Expand Down
15 changes: 11 additions & 4 deletions spec/rubocop/cop/factory_bot/association_style_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@ def inspected_source_filename
context 'when factory block is empty' do
it 'does not register an offense' do
expect_no_offenses(<<~RUBY)
factory :user do
FactoryBot.define do
factory :user do
end
end
RUBY
end
Expand All @@ -24,16 +26,21 @@ def inspected_source_filename
context 'with when factory has no block' do
it 'does not register an offense' do
expect_no_offenses(<<~RUBY)
factory :user
FactoryBot.define do
factory :user
factory :admin_user, parent: :user
end
RUBY
end
end

context 'when implicit style is used' do
it 'does not register an offense' do
expect_no_offenses(<<~RUBY)
factory :article do
user
FactoryBot.define do
factory :article do
user
end
end
RUBY
end
Expand Down

0 comments on commit 4436d57

Please sign in to comment.