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

Inherits:
Cop
  • 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'.freeze
HOOKS =
Hooks::ALL.node_pattern_union.freeze

Constants inherited from Cop

Cop::DEFAULT_CONFIGURATION, Cop::DEFAULT_PATTERN_RE

Constants included from RSpec::Language

RSpec::Language::ALL

Instance Method Summary collapse

Methods inherited from Cop

inherited, #relevant_file?

Instance Method Details

#on_block(node) ⇒ Object



38
39
40
41
42
43
44
45
46
# File 'lib/rubocop/cop/rspec/expect_in_hook.rb', line 38

def on_block(node)
  hook(node) do |hook_name, body|
    expect(body) do |expect|
      method = send_node(expect)
      add_offense(method, location: :selector,
                          message: message(method, hook_name))
    end
  end
end