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

#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



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

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



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

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



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

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



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

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



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

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



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

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



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

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



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

def on_send(node)
  expectation(node) do |expectation, method_name, matcher|
    on_expectation(expectation, method_name, matcher)
  end
end