Class: RuboCop::Cop::Thoughtbot::NoBefore

Inherits:
Base
  • Object
show all
Includes:
SpecGroup
Defined in:
lib/rubocop/cop/thoughtbot/no_before.rb

Overview

Checks for before hooks in specs.

Setup in a before sits away from the examples that use it, so a reader has to jump around the file to work out what any one example is actually doing (the before becomes a mystery guest). Setting data up inside each example keeps the whole story of the test in one place, avoids messy overrides for differing scenarios, and keeps the cost of setup visible.

See https://thoughtbot.com/blog/lets-not

See https://thoughtbot.com/blog/the-arrange-act-assert-pattern

Examples:

# bad
RSpec.describe User do
  before { @user = build(:user) }

  it "is valid" do
    expect(@user).to be_valid
  end
end

# good
RSpec.describe User do
  it "is valid" do
    user = build(:user)

    expect(user).to be_valid
  end
end

Constant Summary collapse

MSG =
"Avoid `before` — set up test data inside each example so it " \
"doesn't become a mystery guest. See https://thoughtbot.com/blog/lets-not"
RESTRICT_ON_SEND =
%i[before].freeze

Constants included from SpecGroup

SpecGroup::SCOPES, SpecGroup::SPEC_GROUPS

Instance Method Summary collapse

Methods included from SpecGroup

#extends_shared_context?, #spec_group?

Instance Method Details

#on_send(node) ⇒ Object Also known as: on_csend



46
47
48
49
50
51
# File 'lib/rubocop/cop/thoughtbot/no_before.rb', line 46

def on_send(node)
  return if node.receiver
  return unless inside_spec_group?(node)

  add_offense(node.loc.selector)
end