Class: RuboCop::Cop::RSpec::ExampleLength

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

Overview

Checks for long examples.

A long example is usually more difficult to understand. Consider extracting out some behaviour, e.g. with a `let` block, or a helper method.

Examples:

# bad
it do
  service = described_class.new
  more_setup
  more_setup
  result = service.call
  expect(result).to be(true)
end

# good
it do
  service = described_class.new
  result = service.call
  expect(result).to be(true)
end

Constant Summary collapse

MSG =
'Example has too many lines [%<total>d/%<max>d].'

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


33
34
35
36
37
38
39
40
41
# File 'lib/rubocop/cop/rspec/example_length.rb', line 33

def on_block(node)
  return unless example?(node)

  length = code_length(node)

  return unless length > max_length

  add_offense(node, message: message(length))
end