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

Inherits:
Cop
  • 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].'

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



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, location: :expression, message: message(length))
end