Class: RuboCop::Cop::RSpec::ScatteredSetup

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

Overview

Checks for setup scattered across multiple hooks in an example group.

Unify ‘before`, `after`, and `around` hooks when possible.

Examples:

# bad
describe Foo do
  before { setup1 }
  before { setup2 }
end

# good
describe Foo do
  before do
    setup1
    setup2
  end
end

Constant Summary collapse

MSG =
'Do not define multiple `%<hook_name>s` hooks in the same ' \
'example group (also defined on %<lines>s).'

Instance Method Summary collapse

Methods inherited from Base

inherited, #on_new_investigation

Methods included from RSpec::Language::NodePattern

#block_pattern, #numblock_pattern, #send_pattern

Methods included from RSpec::Language

#example?, #example_group?, #example_group_with_body?, #hook?, #include?, #let?, #rspec?, #shared_group?, #spec_group?, #subject?

Instance Method Details

#on_block(node) ⇒ Object

rubocop:disable InternalAffairs/NumblockHandler



29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/rubocop/cop/rspec/scattered_setup.rb', line 29

def on_block(node) # rubocop:disable InternalAffairs/NumblockHandler
  return unless example_group?(node)

  repeated_hooks(node).each do |occurrences|
    lines = occurrences.map(&:first_line)

    occurrences.each do |occurrence|
      lines_except_current = lines - [occurrence.first_line]
      message = format(MSG, hook_name: occurrences.first.method_name,
                            lines: lines_msg(lines_except_current))
      add_offense(occurrence, message: message)
    end
  end
end