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

Inherits:
Base
  • Object
show all
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
before { @user = build(:user) }

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

# good
it "is valid" do
  user = build(:user)

  expect(user).to be_valid
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

Instance Method Summary collapse

Instance Method Details

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



40
41
42
43
44
# File 'lib/rubocop/cop/thoughtbot/no_before.rb', line 40

def on_send(node)
  return if node.receiver

  add_offense(node.loc.selector)
end