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 133
def on_block(node)
  return if node.each_ancestor(:def, :defs).any?
  return if node.each_ancestor(:block).any? { |block| example?(block) }

  example_group_body(node) do |body|
    add_offense(node.send_node) if offensive?(body)
  end
end

Private Instance Methods

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

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

  if body.if_type?
    !examples_in_branches?(body)
  else
    !examples?(body)
  end
end