Class: RuboCop::Cop::RSpec::StubbedMock

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

Overview

Checks that message expectations do not have a configured response.

Examples:

# bad
expect(foo).to receive(:bar).with(42).and_return("hello world")

# good (without spies)
allow(foo).to receive(:bar).with(42).and_return("hello world")
expect(foo).to receive(:bar).with(42)

Constant Summary collapse

MSG =
'Prefer `%<replacement>s` over `%<method_name>s` when ' \
'configuring a response.'
RESTRICT_ON_SEND =
%i[to].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

#configured_response?(node) ⇒ Object



42
43
44
45
# File 'lib/rubocop/cop/rspec/stubbed_mock.rb', line 42

def_node_matcher :configured_response?, <<~PATTERN
  { :and_return :and_raise :and_throw :and_yield
    :and_call_original :and_wrap_original }
PATTERN

#expectation(node) {|RuboCop::AST::Node| ... } ⇒ Object

Match expectation

Examples:

source that matches

is_expected.to be_in_the_bar

source that matches

expect(cocktail).to contain_exactly(:fresh_orange_juice, :campari)

source that matches

expect_any_instance_of(Officer).to be_alert

Parameters:

  • node (RuboCop::AST::Node)

Yields:

  • (RuboCop::AST::Node)

    expectation, method name, matcher



61
62
63
64
65
# File 'lib/rubocop/cop/rspec/stubbed_mock.rb', line 61

def_node_matcher :expectation, <<~PATTERN
  (send
    $(send nil? $#Expectations.all ...)
    :to $_)
PATTERN

#matcher_with_blockpass(node) {|RuboCop::AST::Node| ... } ⇒ Object

Match matcher with a configured response in block-pass

Examples:

source that matches

receive(:foo, &canned)

source that matches

receive_message_chain(:foo, :bar, &canned)

source that matches

receive(:foo).with('bar', &canned)

Parameters:

  • node (RuboCop::AST::Node)

Yields:

  • (RuboCop::AST::Node)

    matcher



129
130
131
132
133
134
# File 'lib/rubocop/cop/rspec/stubbed_mock.rb', line 129

def_node_matcher :matcher_with_blockpass, <<~PATTERN
  {
    (send nil? { :receive :receive_message_chain } ... block_pass)  # receive(:foo, &canned)
    (send (send nil? :receive ...) :with ... block_pass)            # receive(:foo).with('foo', &canned)
  }
PATTERN

#matcher_with_configured_response(node) {|RuboCop::AST::Node| ... } ⇒ Object

Match matcher with a configured response

Examples:

source that matches

receive(:foo).and_return('bar')

source that matches

receive(:lower).and_raise(SomeError)

source that matches

receive(:redirect).and_call_original

Parameters:

  • node (RuboCop::AST::Node)

Yields:

  • (RuboCop::AST::Node)

    matcher



81
82
83
# File 'lib/rubocop/cop/rspec/stubbed_mock.rb', line 81

def_node_matcher :matcher_with_configured_response, <<~PATTERN
  (send #message_expectation? #configured_response? _)
PATTERN

#matcher_with_hash(node) {|RuboCop::AST::Node| ... } ⇒ Object

Match matcher with a configured response defined as a hash

Examples:

source that matches

receive_messages(foo: 'bar', baz: 'qux')

source that matches

receive_message_chain(:foo, bar: 'baz')

Parameters:

  • node (RuboCop::AST::Node)

Yields:

  • (RuboCop::AST::Node)

    matcher



108
109
110
111
112
113
# File 'lib/rubocop/cop/rspec/stubbed_mock.rb', line 108

def_node_matcher :matcher_with_hash, <<~PATTERN
  {
    (send nil? :receive_messages hash)           # receive_messages(foo: 'bar', baz: 'qux')
    (send nil? :receive_message_chain ... hash)  # receive_message_chain(:foo, bar: 'baz')
  }
PATTERN

#matcher_with_return_block(node) {|RuboCop::AST::Node| ... } ⇒ Object

Match matcher with a return block

Examples:

source that matches

receive(:foo) { 'bar' }

Parameters:

  • node (RuboCop::AST::Node)

Yields:

  • (RuboCop::AST::Node)

    matcher



93
94
95
# File 'lib/rubocop/cop/rspec/stubbed_mock.rb', line 93

def_node_matcher :matcher_with_return_block, <<~PATTERN
  (block #message_expectation? (args) _)  # receive(:foo) { 'bar' }
PATTERN

#message_expectation?(node) ⇒ Array<RuboCop::AST::Node>

Match message expectation matcher

Examples:

source that matches

receive(:foo)

source that matches

receive_message_chain(:foo, :bar)

source that matches

receive(:foo).with('bar')

Parameters:

  • node (RuboCop::AST::Node)

Returns:

  • (Array<RuboCop::AST::Node>)

    matching nodes



34
35
36
37
38
39
# File 'lib/rubocop/cop/rspec/stubbed_mock.rb', line 34

def_node_matcher :message_expectation?, <<~PATTERN
  {
    (send nil? { :receive :receive_message_chain } ...)  # receive(:foo)
    (send (send nil? :receive ...) :with ...)            # receive(:foo).with('bar')
  }
PATTERN

#on_send(node) ⇒ Object



138
139
140
# File 'lib/rubocop/cop/rspec/stubbed_mock.rb', line 138

def on_send(node)
  expectation(node, &method(:on_expectation))
end