Class: RuboCop::Cop::RSpec::VerifiedDoubles

Inherits:
Base
  • Object
show all
Defined in:
lib/rubocop/cop/rspec/verified_doubles.rb

Overview

Prefer using verifying doubles over normal doubles.

Examples:

# bad
let(:foo) do
  double(method_name: 'returned value')
end

# bad
let(:foo) do
  double("ClassName", method_name: 'returned value')
end

# good
let(:foo) do
  instance_double("ClassName", method_name: 'returned value')
end

See Also:

Constant Summary collapse

MSG =
'Prefer using verifying doubles over normal doubles.'
RESTRICT_ON_SEND =
%i[double spy].freeze

Instance Method Summary collapse

Methods inherited from Base

inherited, #on_new_investigation

Methods included from RSpec::Language::NodePattern

#block_or_numblock_pattern, #block_pattern, #numblock_pattern, #send_pattern

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



35
36
37
38
39
40
41
42
# File 'lib/rubocop/cop/rspec/verified_doubles.rb', line 35

def on_send(node)
  unverified_double(node) do |name, *_args|
    return if name.nil? && cop_config['IgnoreNameless']
    return if symbol?(name) && cop_config['IgnoreSymbolicNames']

    add_offense(node)
  end
end

#unverified_double(node) ⇒ Object



31
32
33
# File 'lib/rubocop/cop/rspec/verified_doubles.rb', line 31

def_node_matcher :unverified_double, <<~PATTERN
  {(send nil? {:double :spy} $...)}
PATTERN