Class: RuboCop::Cop::RSpec::VerifiedDoubleReference

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

Overview

Checks for consistent verified double reference style.

Only investigates references that are one of the supported styles.

This cop can be configured in your configuration using the ‘EnforcedStyle` option and supports `–auto-gen-config`.

Examples:

‘EnforcedStyle: constant` (default)

# bad
let(:foo) do
  instance_double('ClassName', method_name: 'returned_value')
end

# good
let(:foo) do
  instance_double(ClassName, method_name: 'returned_value')
end

‘EnforcedStyle: string`

# bad
let(:foo) do
  instance_double(ClassName, method_name: 'returned_value')
end

# good
let(:foo) do
  instance_double('ClassName', method_name: 'returned_value')
end

Reference is not in the supported style list. No enforcement


# good
let(:foo) do
  instance_double(@klass, method_name: 'returned_value')
end

See Also:

Constant Summary collapse

MSG =
'Use a %<style>s class reference for verified doubles.'
RESTRICT_ON_SEND =
Set[
  :class_double,
  :class_spy,
  :instance_double,
  :instance_spy,
  :mock_model,
  :object_double,
  :object_spy,
  :stub_model
].freeze
REFERENCE_TYPE_STYLES =
{
  str: :string,
  const: :constant
}.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

#on_send(node) ⇒ Object



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/rubocop/cop/rspec/verified_double_reference.rb', line 74

def on_send(node)
  verified_double(node) do |class_reference|
    break correct_style_detected unless opposing_style?(class_reference)

    message = format(MSG, style: style)
    expression = class_reference.source_range

    add_offense(expression, message: message) do |corrector|
      offense = class_reference.source
      corrector.replace(expression, correct_style(offense))

      opposite_style_detected
    end
  end
end

#verified_double(node) ⇒ Object



66
67
68
69
70
71
72
# File 'lib/rubocop/cop/rspec/verified_double_reference.rb', line 66

def_node_matcher :verified_double, <<~PATTERN
  (send
    nil?
    RESTRICT_ON_SEND
    $_class_reference
    ...)
PATTERN