Class: RuboCop::Cop::RSpec::RepeatedExampleGroupBody

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

Overview

Check for repeated describe and context block body.

Examples:


# bad
describe 'cool feature x' do
  it { cool_predicate }
end

describe 'cool feature y' do
  it { cool_predicate }
end

# good
describe 'cool feature' do
  it { cool_predicate }
end

describe 'another cool feature' do
  it { another_predicate }
end

# good
context 'when case x', :tag do
  it { cool_predicate }
end

context 'when case y' do
  it { cool_predicate }
end

# good
context Array do
  it { is_expected.to respond_to :each }
end

context Hash do
  it { is_expected.to respond_to :each }
end

Constant Summary collapse

MSG =
'Repeated %<group>s block body on line(s) %<loc>s'

Instance Method Summary collapse

Methods inherited from Base

inherited, #on_new_investigation

Methods included from RSpec::Language::NodePattern

#block_pattern, #send_pattern

Instance Method Details

#on_begin(node) ⇒ Object


61
62
63
64
65
66
67
# File 'lib/rubocop/cop/rspec/repeated_example_group_body.rb', line 61

def on_begin(node)
  return unless several_example_groups?(node)

  repeated_group_bodies(node).each do |group, repeats|
    add_offense(group, message: message(group, repeats))
  end
end