Class: RuboCop::Cop::Vicenzo::RSpec::TopLevelExample

Inherits:
RSpec::Base
  • Object
show all
Includes:
TopLevelDefinition
Defined in:
lib/rubocop/cop/vicenzo/rspec/top_level_example.rb

Overview

An example declared straight inside the top-level example group is about no behaviour in particular. Move it into the group describing the behaviour it exercises.

The top-level group names the object under test, not one of the things it does. An example hanging directly off it therefore reads as a statement about the object as a whole, when it is always a statement about one behaviour - the group naming that behaviour is simply missing, and with it the place where the next example about the same behaviour would go.

It is also the shape that keeps premises in the top-level group alive: as long as examples live at the root, the subject and the lets they read have nowhere else to be declared, which is what Vicenzo/RSpec/TopLevelSubject and Vicenzo/RSpec/TopLevelLet report.

Examples:

# bad - the example is about `#adult?`, but no group says so
RSpec.describe Person do
  it { is_expected.to be_adult }
end

# good - the behaviour the example is about has a name, and a home
RSpec.describe Person do
  describe '#adult?' do
    subject(:person) { described_class.new(age: 18) }

    it { is_expected.to be_adult }
  end
end

Constant Summary collapse

MSG =
'This example is declared in the top-level example group, which names no behaviour. ' \
'Move it into the group describing the behaviour it exercises.'

Constants included from PremiseTracking

PremiseTracking::SETUP_HOOKS

Method Summary

Methods included from TopLevelDefinition

#on_block

Methods included from PremiseTracking

#premise_name, #root_receiver_name