Class: RuboCop::Cop::RSpec::EmptyExampleGroup

Inherits:
Cop
  • Object
show all
Defined in:
lib/rubocop/cop/rspec/empty_example_group.rb

Overview

Checks if an example group does not include any tests.

This cop is configurable using the ‘CustomIncludeMethods` option

Examples:

usage


# bad
describe Bacon do
  let(:bacon)      { Bacon.new(chunkiness) }
  let(:chunkiness) { false                 }

  context 'extra chunky' do   # flagged by rubocop
    let(:chunkiness) { true }
  end

  it 'is chunky' do
    expect(bacon.chunky?).to be_truthy
  end
end

# good
describe Bacon do
  let(:bacon)      { Bacon.new(chunkiness) }
  let(:chunkiness) { false                 }

  it 'is chunky' do
    expect(bacon.chunky?).to be_truthy
  end
end

configuration


# .rubocop.yml
# RSpec/EmptyExampleGroup:
#   CustomIncludeMethods:
#   - include_tests

# spec_helper.rb
RSpec.configure do |config|
  config.alias_it_behaves_like_to(:include_tests)
end

# bacon_spec.rb
describe Bacon do
  let(:bacon)      { Bacon.new(chunkiness) }
  let(:chunkiness) { false                 }

  context 'extra chunky' do   # not flagged by rubocop
    let(:chunkiness) { true }

    include_tests 'shared tests'
  end
end

Constant Summary collapse

MSG =
'Empty example group detected.'

Constants inherited from Cop

Cop::DEFAULT_CONFIGURATION, Cop::DEFAULT_PATTERN_RE

Constants included from RSpec::Language

RSpec::Language::ALL, RSpec::Language::RSPEC

Instance Method Summary collapse

Methods inherited from Cop

inherited, #relevant_file?

Instance Method Details

#on_block(node) ⇒ Object



70
71
72
73
74
# File 'lib/rubocop/cop/rspec/empty_example_group.rb', line 70

def on_block(node)
  return unless example_group?(node) && !contains_example?(node)

  add_offense(node.send_node, location: :expression)
end