Class: RuboCop::Cop::Captive::RSpec::SpecifyBeforeParameter

Inherits:
Base
  • Object
show all
Extended by:
AutoCorrector
Defined in:
lib/rubocop/cop/captive/rspec/specify_before_parameter.rb

Overview

This cop enforces specifying the parameter in RSpec ‘before` blocks.

The cops was created because many people didn’t know that ‘before` method has default parameter value : `:each`

Examples:

# bad
before do
  ...
end

# good
before(:each) do
  ...
end

before(:all) do
  ...
end

Constant Summary collapse

MSG =
"Specify the parameter in `before` blocks. Example : before(:each) or before(:all)"

Instance Method Summary collapse

Instance Method Details

#on_block(node) ⇒ Object



43
44
45
46
47
48
49
50
# File 'lib/rubocop/cop/captive/rspec/specify_before_parameter.rb', line 43

def on_block(node)
  return unless before_block?(node)
  return unless before_without_parameter?(node)

  add_offense(node, message: MSG) do |corrector|
    corrector.replace(node.loc.expression, add_parameter(node))
  end
end