Class: RuboCop::Cop::RSpec::ExpectInHook

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

Overview

Do not use ‘expect` in hooks such as `before`.

Examples:

# bad
before do
  expect(something).to eq 'foo'
end

# bad
after do
  expect_any_instance_of(Something).to receive(:foo)
end

# good
it do
  expect(something).to eq 'foo'
end

Constant Summary collapse

MSG =
'Do not use `%<expect>s` in `%<hook>s` hook'

Instance Method Summary collapse

Methods inherited from Base

inherited, #on_new_investigation

Methods included from RSpec::Language::NodePattern

#block_or_numblock_pattern, #block_pattern, #numblock_pattern, #send_pattern

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

#expectation(node) ⇒ Object



28
# File 'lib/rubocop/cop/rspec/expect_in_hook.rb', line 28

def_node_search :expectation, '(send nil? #Expectations.all ...)'

#on_block(node) ⇒ Object Also known as: on_numblock

rubocop:disable InternalAffairs/NumblockHandler



30
31
32
33
34
35
36
37
38
# File 'lib/rubocop/cop/rspec/expect_in_hook.rb', line 30

def on_block(node) # rubocop:disable InternalAffairs/NumblockHandler
  return unless hook?(node)
  return if node.body.nil?

  expectation(node.body) do |expect|
    add_offense(expect.loc.selector,
                message: message(expect, node))
  end
end