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'

Constants inherited from Cop

Cop::DEFAULT_CONFIGURATION, Cop::DEFAULT_PATTERN_RE

Constants included from RSpec::Language

RSpec::Language::ALL, RSpec::Language::RSPEC

Instance Method Summary collapse

Methods inherited from Cop

inherited, #relevant_file?

Instance Method Details

#on_block(node) ⇒ Object



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

def on_block(node)
  return unless hook?(node)
  return if node.body.nil?

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