Class: RuboCop::Cop::RSpec::Yield

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

Overview

This cop checks for calling a block within a stub.

Examples:

# bad
allow(foo).to receive(:bar) { |&block| block.call(1) }

# good
expect(foo).to be(:bar).and_yield(1)

Constant Summary collapse

MSG =
'Use `.and_yield`.'

Instance Method Summary collapse

Methods inherited from Base

inherited, #on_new_investigation

Methods included from RSpec::Language::NodePattern

#block_pattern, #send_pattern

Instance Method Details

#on_block(node) ⇒ Object


26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/rubocop/cop/rspec/yield.rb', line 26

def on_block(node)
  return unless method_on_stub?(node.send_node)

  block_arg(node.arguments) do |block|
    if calling_block?(node.body, block)
      range = block_range(node)

      add_offense(range) do |corrector|
        autocorrect(corrector, node, range)
      end
    end
  end
end