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

Inherits:
RuboCop::Cop show all
Includes:
CodeLength
Defined in:
lib/rubocop/cop/rspec/example_length.rb

Overview

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

EXAMPLE_BLOCKS =
[:it, :specify].freeze

Instance Method Summary collapse

Instance Method Details

#on_block(node) ⇒ Object



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

def on_block(node)
  method, _args, _body = *node
  _receiver, method_name, _object = *method
  return unless EXAMPLE_BLOCKS.include?(method_name)

  length = code_length(node)

  return unless length > max
  add_offense(node, :expression, message(length, max))
end