Class: RuboCop::Cop::RSpec::StringAsInstanceDoubleConstant

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

Overview

Do not use a string as ‘instance_double` constant.

Examples:

# bad
instance_double('User', name: 'John')

# good
instance_double(User, name: 'John')

Constant Summary collapse

MSG =
'Do not use a string as `instance_double` constant.'
RESTRICT_ON_SEND =
%i[instance_double].freeze

Instance Method Summary collapse

Methods inherited from Base

inherited, #on_new_investigation

Methods included from RSpec::Language

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

Instance Method Details

#autocorrect(corrector, node) ⇒ Object



39
40
41
# File 'lib/rubocop/cop/rspec/string_as_instance_double_constant.rb', line 39

def autocorrect(corrector, node)
  corrector.replace(node, node.value)
end

#on_send(node) ⇒ Object



31
32
33
34
35
36
37
# File 'lib/rubocop/cop/rspec/string_as_instance_double_constant.rb', line 31

def on_send(node)
  stringified_instance_double_const?(node) do |args_node|
    add_offense(args_node) do |corrector|
      autocorrect(corrector, args_node)
    end
  end
end

#stringified_instance_double_const?(node) ⇒ Object



27
28
29
# File 'lib/rubocop/cop/rspec/string_as_instance_double_constant.rb', line 27

def_node_matcher :stringified_instance_double_const?, <<~PATTERN
  (send nil? :instance_double $str ...)
PATTERN