Class: RuboCop::Cop::FactoryBot::AssociationStyle

Inherits:
Base
  • Object
show all
Extended by:
AutoCorrector
Includes:
ConfigurableEnforcedStyle
Defined in:
lib/rubocop/cop/factory_bot/association_style.rb

Overview

Use a consistent style to define associations.

Examples:

EnforcedStyle: implicit (default)

# bad
factory :post do
  association :user
end

# good
factory :post do
  user
end

# bad
factory :post do
  association :user, :author
end

# good
factory :post do
  user factory: %i[user author]
end

EnforcedStyle: explicit

# bad
factory :post do
  user
end

# good
factory :post do
  association :user
end

# bad
factory :post do
  user factory: %i[user author]
end

# good
factory :post do
  association :user, :author
end

# good (NonImplicitAssociationMethodNames: ['email'])
sequence :email do |n|
  "person#{n}@example.com"
end

factory :user do
  email
end

Constant Summary collapse

DEFAULT_NON_IMPLICIT_ASSOCIATION_METHOD_NAMES =
%w[
  association
  factory
  sequence
  skip_create
  traits_for_enum
].freeze
RESTRICT_ON_SEND =
%i[factory trait].freeze
KEYWORDS =
%i[alias and begin break case class def defined? do
else elsif end ensure false for if in module
next nil not or redo rescue retry return self
super then true undef unless until when while
yield __FILE__ __LINE__ __ENCODING__].freeze

Instance Method Summary collapse

Instance Method Details

#on_send(node) ⇒ Object



83
84
85
86
87
88
89
90
91
92
# File 'lib/rubocop/cop/factory_bot/association_style.rb', line 83

def on_send(node)
  bad_associations_in(node).each do |association|
    add_offense(
      association,
      message: "Use #{style} style to define associations."
    ) do |corrector|
      autocorrect(corrector, association)
    end
  end
end