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

Inherits:
RuboCop::Cop show all
Includes:
CodeLength, RSpec::SpecOnly
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

EXAMPLE_BLOCKS =
RuboCop::RSpec::Language::Examples::ALL

Constants included from RSpec::SpecOnly

RSpec::SpecOnly::DEFAULT_CONFIGURATION

Instance Method Summary collapse

Methods included from RSpec::SpecOnly

#relevant_file?

Instance Method Details

#on_block(node) ⇒ Object



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

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