Class: RuboCop::Cop::RSpec::ImplicitBlockExpectation

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

Overview

Check that implicit block expectation syntax is not used.

Prefer using explicit block expectations.

Examples:

# 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

Constant Summary collapse

MSG =
'Avoid implicit block expectations.'

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_send(node) ⇒ Object


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

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