Class: RuboCop::Cop::RSpec::FactoryBot::AttributeDefinedStatically

Inherits:
Base
  • Object
show all
Extended by:
AutoCorrector
Defined in:
lib/rubocop/cop/rspec/factory_bot/attribute_defined_statically.rb

Overview

Always declare attribute values as blocks.

Examples:

# bad
kind [:active, :rejected].sample

# good
kind { [:active, :rejected].sample }

# bad
closed_at 1.day.from_now

# good
closed_at { 1.day.from_now }

# bad
count 1

# good
count { 1 }

Constant Summary collapse

MSG =
'Use a block to declare attribute values.'

Constants inherited from Base

Base::DEFAULT_CONFIGURATION, Base::DEFAULT_PATTERN_RE

Constants included from RSpec::Language

RSpec::Language::ALL

Instance Method Summary collapse

Methods inherited from Base

inherited, #relevant_file?

Instance Method Details

#on_block(node) ⇒ Object


40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/rubocop/cop/rspec/factory_bot/attribute_defined_statically.rb', line 40

def on_block(node)
  attributes = factory_attributes(node) || []
  attributes = [attributes] unless attributes.is_a?(Array) # rubocop:disable Style/ArrayCoercion, Lint/RedundantCopDisableDirective

  attributes.each do |attribute|
    next unless offensive_receiver?(attribute.receiver, node)
    next if proc?(attribute) || association?(attribute.first_argument)

    add_offense(attribute) do |corrector|
      autocorrect(corrector, attribute)
    end
  end
end