Class: RuboCop::Cop::RSpec::DiscardedMatcher

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

Overview

Checks for matchers that are used in void context.

Matcher calls like ‘change`, `receive`, etc. that appear as standalone expressions have their result silently discarded. This usually means a missing `.and` to chain compound matchers.

The list of matcher methods can be configured with ‘CustomMatcherMethods`.

Examples:

# bad
specify do
  expect { result }
    .to change { obj.foo }.from(1).to(2)
    change { obj.bar }.from(3).to(4)
end

# good
specify do
  expect { result }
    .to change { obj.foo }.from(1).to(2)
    .and change { obj.bar }.from(3).to(4)
end

# good
specify do
  expect { result }.to change { obj.foo }.from(1).to(2)
end

Constant Summary collapse

MSG =
'The result of `%<method>s` is not used. ' \
'Did you mean to chain it with `.and`?'
MATCHER_METHODS =
%i[
  change have_received output
  receive receive_messages receive_message_chain
].to_set.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_block(node) ⇒ Object

rubocop:disable InternalAffairs/NumblockHandler, InternalAffairs/ItblockHandler



50
51
52
# File 'lib/rubocop/cop/rspec/discarded_matcher.rb', line 50

def on_block(node) # rubocop:disable InternalAffairs/NumblockHandler, InternalAffairs/ItblockHandler
  check_discarded_matcher(node.send_node, node)
end

#on_send(node) ⇒ Object



46
47
48
# File 'lib/rubocop/cop/rspec/discarded_matcher.rb', line 46

def on_send(node)
  check_discarded_matcher(node, node)
end