Class: RuboCop::Cop::RSpec::FactoryBot::FactoryNameStyle

Inherits:
Base
  • Object
show all
Extended by:
AutoCorrector
Includes:
ConfigurableEnforcedStyle, RSpec::FactoryBot::Language
Defined in:
lib/rubocop/cop/rspec/factory_bot/factory_name_style.rb

Overview

Checks for name style for argument of FactoryBot::Syntax::Methods.

Examples:

EnforcedStyle: symbol (default)

# bad
create('user')
build "user", username: "NAME"

# good
create(:user)
build :user, username: "NAME"

EnforcedStyle: string

# bad
create(:user)
build :user, username: "NAME"

# good
create('user')
build "user", username: "NAME"

Constant Summary collapse

MSG =
'Use %<prefer>s to refer to a factory.'
FACTORY_CALLS =
RuboCop::RSpec::FactoryBot::Language::METHODS
RESTRICT_ON_SEND =
FACTORY_CALLS

Constants included from RSpec::FactoryBot::Language

RSpec::FactoryBot::Language::METHODS

Instance Method Summary collapse

Methods included from RSpec::FactoryBot::Language

#factory_bot?

Instance Method Details

#factory_call(node) ⇒ Object



37
38
39
40
41
42
# File 'lib/rubocop/cop/rspec/factory_bot/factory_name_style.rb', line 37

def_node_matcher :factory_call, <<-PATTERN
(send
  {#factory_bot? nil?} %FACTORY_CALLS
  ${str sym} ...
)
PATTERN

#on_send(node) ⇒ Object



44
45
46
47
48
49
50
51
52
# File 'lib/rubocop/cop/rspec/factory_bot/factory_name_style.rb', line 44

def on_send(node)
  factory_call(node) do |name|
    if offense_for_symbol_style?(name)
      register_offense(name, name.value.to_sym.inspect)
    elsif offense_for_string_style?(name)
      register_offense(name, name.value.to_s.inspect)
    end
  end
end