class RuboCop::Cop::RSpec::RepeatedDescription

Check for repeated description strings in example groups.

@example

# bad
RSpec.describe User do
  it 'is valid' do
    # ...
  end

  it 'is valid' do
    # ...
  end
end

# good
RSpec.describe User do
  it 'is valid when first and last name are present' do
    # ...
  end

  it 'is valid when last name only is present' do
    # ...
  end
end

# good
RSpec.describe User do
  it 'is valid' do
    # ...
  end

  it 'is valid', :flag do
    # ...
  end
end

Constants

MSG

Public Instance Methods

on_block(node) click to toggle source
# File lib/rubocop/cop/rspec/repeated_description.rb, line 46
def on_block(node)
  return unless example_group?(node)

  repeated_descriptions(node).each do |repeated_description|
    add_offense(repeated_description)
  end
end

Private Instance Methods

example_signature(example) click to toggle source
# File lib/rubocop/cop/rspec/repeated_description.rb, line 70
def example_signature(example)
  [example.metadata, example.doc_string]
end
repeated_descriptions(node) click to toggle source

Select examples in the current scope with repeated description strings

# File lib/rubocop/cop/rspec/repeated_description.rb, line 57
def repeated_descriptions(node)
  grouped_examples =
    RuboCop::RSpec::ExampleGroup.new(node)
      .examples
      .group_by { |example| example_signature(example) }

  grouped_examples
    .select { |signatures, group| signatures.any? && group.size > 1 }
    .values
    .flatten
    .map(&:definition)
end