Class: RuboCop::Cop::RSpec::RepeatedIncludeExample

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

Overview

Check for repeated include of shared examples.

Examples:


# bad
describe 'foo' do
  include_examples 'cool stuff'
  include_examples 'cool stuff'
end

# bad
describe 'foo' do
  it_behaves_like 'a cool', 'thing'
  it_behaves_like 'a cool', 'thing'
end

# bad
context 'foo' do
  it_should_behave_like 'a duck'
  it_should_behave_like 'a duck'
end

# good
describe 'foo' do
  include_examples 'cool stuff'
end

describe 'bar' do
  include_examples 'cool stuff'
end

# good
describe 'foo' do
  it_behaves_like 'a cool', 'thing'
  it_behaves_like 'a cool', 'person'
end

# good
context 'foo' do
  it_should_behave_like 'a duck'
  it_should_behave_like 'a goose'
end

Constant Summary collapse

MSG =
'Repeated include of shared_examples %<name>s ' \
'on line(s) %<repeat>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



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

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

  repeated_include_examples(node).each do |item, repeats|
    add_offense(item, message: message(item, repeats))
  end
end