Class: RuboCop::Cop::RSpec::FactoryBot::SyntaxMethods

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

Overview

Use shorthands from ‘FactoryBot::Syntax::Methods` in your specs.

Examples:

# bad
FactoryBot.create(:bar)
FactoryBot.build(:bar)
FactoryBot.attributes_for(:bar)

# good
create(:bar)
build(:bar)
attributes_for(:bar)

Constant Summary collapse

MSG =
'Use `%<method>s` from `FactoryBot::Syntax::Methods`.'
RESTRICT_ON_SEND =
RuboCop::RSpec::FactoryBot::Language::METHODS

Constants included from RSpec::FactoryBot::Language

RSpec::FactoryBot::Language::METHODS

Instance Method Summary collapse

Methods included from RSpec::FactoryBot::Language

#factory_bot?

Methods inherited from Base

inherited, #on_new_investigation

Methods included from RSpec::Language::NodePattern

#block_pattern, #numblock_pattern, #send_pattern

Methods included from RSpec::Language

#example?, #example_group?, #example_group_with_body?, #hook?, #include?, #let?, #rspec?, #shared_group?, #spec_group?, #subject?

Instance Method Details

#on_send(node) ⇒ Object



59
60
61
62
63
64
65
66
67
68
# File 'lib/rubocop/cop/rspec/factory_bot/syntax_methods.rb', line 59

def on_send(node)
  return unless factory_bot?(node.receiver)
  return unless inside_example_group?(node)

  message = format(MSG, method: node.method_name)

  add_offense(crime_scene(node), message: message) do |corrector|
    corrector.remove(offense(node))
  end
end