Class: Rubocop::Cop::RSpec::FeatureSpecMaxExamples

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

Overview

Limits the number of examples in feature spec files.

Feature specs with too many examples are slow to run, hard to maintain, and often indicate the file should be split into smaller, focused specs.

The maximum number of examples can be configured with the ‘Max` option (default: 25).

Examples:

Max: 25 (default)


# bad
# A feature spec file with more than 25 examples.

# good
# A feature spec file with 25 or fewer examples.

Constant Summary collapse

MSG =
'Feature spec file has %<count>d examples, which exceeds the maximum of %<max>d. ' \
'Consider splitting into smaller, focused files grouped by user flow or feature area.'

Instance Method Summary collapse

Instance Method Details

#on_block(node) ⇒ Object Also known as: on_numblock



34
35
36
# File 'lib/rubocop/cop/rspec/feature_spec_max_examples.rb', line 34

def on_block(node)
  @example_count += 1 if example?(node)
end

#on_investigation_endObject



39
40
41
# File 'lib/rubocop/cop/rspec/feature_spec_max_examples.rb', line 39

def on_investigation_end
  add_global_offense(format(MSG, count: @example_count, max: @max)) if @example_count > @max
end

#on_new_investigationObject



28
29
30
31
32
# File 'lib/rubocop/cop/rspec/feature_spec_max_examples.rb', line 28

def on_new_investigation
  super
  @example_count = 0
  @max = cop_config['Max'] || 25
end