class RuboCop::Cop::RSpec::EmptyExampleGroup

Checks if an example group does not include any tests.

@example 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

# good
describe Bacon do
  pending 'will add tests later'
end

Constants

MSG

Public Instance Methods

on_block(node) click to toggle source
# File lib/rubocop/cop/rspec/empty_example_group.rb, line 138
def on_block(node) # rubocop:disable InternalAffairs/NumblockHandler
  return if node.each_ancestor(:def, :defs).any?
  return if node.each_ancestor(:block).any? { |block| example?(block) }

  example_group_body(node) do |body|
    next unless offensive?(body)

    add_offense(node.send_node) do |corrector|
      corrector.remove(removed_range(node))
    end
  end
end

Private Instance Methods

conditionals_with_examples?(body) click to toggle source
# File lib/rubocop/cop/rspec/empty_example_group.rb, line 164
def conditionals_with_examples?(body)
  return false unless body.begin_type? || body.case_type?

  body.each_descendant(:if, :case).any? do |condition_node|
    examples_in_branches?(condition_node)
  end
end
examples_in_branches?(condition_node) click to toggle source
# File lib/rubocop/cop/rspec/empty_example_group.rb, line 172
def examples_in_branches?(condition_node)
  return false if !condition_node.if_type? && !condition_node.case_type?

  condition_node.branches.any? { |branch| examples?(branch) }
end
offensive?(body) click to toggle source
# File lib/rubocop/cop/rspec/empty_example_group.rb, line 153
def offensive?(body)
  return true unless body
  return false if conditionals_with_examples?(body)

  if body.if_type? || body.case_type?
    !examples_in_branches?(body)
  else
    !examples?(body)
  end
end
removed_range(node) click to toggle source
# File lib/rubocop/cop/rspec/empty_example_group.rb, line 178
def removed_range(node)
  range_by_whole_lines(
    node.source_range,
    include_final_newline: true
  )
end