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

Inherits:
RuboCop::Cop
  • Object
show all
Includes:
RSpec::Language, RSpec::Language::NodePattern, RSpec::SpecOnly
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.'.freeze

Constants included from RSpec::SpecOnly

RSpec::SpecOnly::DEFAULT_CONFIGURATION

Constants included from RSpec::Language

RSpec::Language::ALL

Instance Method Summary collapse

Methods included from RSpec::SpecOnly

#relevant_file?

Instance Method Details

#on_block(node) ⇒ Object



80
81
82
83
84
# File 'lib/rubocop/cop/rspec/empty_example_group.rb', line 80

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

  add_offense(node.children.first, :expression)
end