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

Inherits:
Base
  • Object
show all
Includes:
SkipOrPending
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 included from SkipOrPending

#skip_or_pending_inside_block?, #skipped_in_metadata?

Methods inherited from Base

inherited, #on_new_investigation

Methods included from RSpec::Language

#example?, #example_group?, #example_group_with_body?, #explicit_rspec?, #hook?, #include?, #let?, #rspec?, #shared_group?, #spec_group?, #subject?

Instance Method Details

#body(node) ⇒ Object



59
# File 'lib/rubocop/cop/rspec/repeated_example_group_body.rb', line 59

def_node_matcher :body, '(block _ args $...)'

#const_arg(node) ⇒ Object



62
# File 'lib/rubocop/cop/rspec/repeated_example_group_body.rb', line 62

def_node_matcher :const_arg, '(block (send _ _ $const ...) ...)'

#metadata(node) ⇒ Object



56
# File 'lib/rubocop/cop/rspec/repeated_example_group_body.rb', line 56

def_node_matcher :metadata, '(block (send _ _ _ $...) ...)'

#on_begin(node) ⇒ Object



64
65
66
67
68
69
70
# File 'lib/rubocop/cop/rspec/repeated_example_group_body.rb', line 64

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

#several_example_groups?(node) ⇒ Object



51
52
53
# File 'lib/rubocop/cop/rspec/repeated_example_group_body.rb', line 51

def_node_matcher :several_example_groups?, <<~PATTERN
  (begin <#example_group_with_body? #example_group_with_body? ...>)
PATTERN