Class: RuboCop::Cop::RSpec::EmptyExample

Inherits:
Base
  • Object
show all
Extended by:
AutoCorrector
Includes:
RangeHelp
Defined in:
lib/rubocop/cop/rspec/empty_example_cops.rb

Constant Summary collapse

MSG =

Checks if an example does not include any expectations.

Examples:

usage


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

  it 'is chunky' do
    bacon.chunky?
  end
end

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

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

Instance Method Summary collapse

Instance Method Details

#on_block(node) ⇒ Object



53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/rubocop/cop/rspec/empty_example_cops.rb', line 53

def on_block(node)
  description = it_description(node)
  return if description.nil?

  unless example_present?(node)
    add_offense(node, message: MSG) do |corrector|
      corrector.replace(
        range_by_whole_lines(node.location.expression),
        "it #{description.last&.source}"
      )
    end
  end
end