class RuboCop::Cop::RSpec::ImplicitBlockExpectation

Check that implicit block expectation syntax is not used.

Prefer using explicit block expectations.

@example

# bad
subject { -> { do_something } }
it { is_expected.to change(something).to(new_value) }

# good
it 'changes something to a new value' do
  expect { do_something }.to change(something).to(new_value)
end

Constants

MSG
RESTRICT_ON_SEND

Public Instance Methods

on_send(node) click to toggle source
# File lib/rubocop/cop/rspec/implicit_block_expectation.rb, line 39
def on_send(node)
  implicit_expect(node) do |implicit_expect|
    subject = nearest_subject(implicit_expect)
    add_offense(implicit_expect) if lambda_subject?(subject&.body)
  end
end

Private Instance Methods

find_subject(block_node) click to toggle source
# File lib/rubocop/cop/rspec/implicit_block_expectation.rb, line 61
def find_subject(block_node)
  block_node.body.child_nodes.find { |send_node| subject?(send_node) }
end
multi_statement_example_group?(node) click to toggle source
# File lib/rubocop/cop/rspec/implicit_block_expectation.rb, line 57
def multi_statement_example_group?(node)
  example_group_with_body?(node) && node.body.begin_type?
end
nearest_subject(node) click to toggle source
# File lib/rubocop/cop/rspec/implicit_block_expectation.rb, line 48
def nearest_subject(node)
  node
    .each_ancestor(:block)
    .lazy
    .select { |block_node| multi_statement_example_group?(block_node) }
    .map { |block_node| find_subject(block_node) }
    .find(&:itself)
end