Class: RuboCop::Cop::RSpec::SubjectDeclaration

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

Overview

Ensure that subject is defined using subject helper.

Examples:

# bad
let(:subject) { foo }
let!(:subject) { foo }
subject(:subject) { foo }
subject!(:subject) { foo }

# bad
block = -> {}
let(:subject, &block)

# good
subject(:test_subject) { foo }

Constant Summary collapse

MSG_LET =
'Use subject explicitly rather than using let'
MSG_REDUNDANT =
'Ambiguous declaration of subject'

Instance Method Summary collapse

Methods inherited from Base

inherited, #on_new_investigation

Methods included from RSpec::Language

#example?, #example_group?, #example_group_with_body?, #explicit_rspec?, #hook?, #include?, #let?, #rspec?, #shared_group?, #spec_group?, #subject?

Instance Method Details

#offensive_subject_declaration?(node) ⇒ Object



29
30
31
# File 'lib/rubocop/cop/rspec/subject_declaration.rb', line 29

def_node_matcher :offensive_subject_declaration?, <<~PATTERN
  (send nil? ${#Subjects.all #Helpers.all} ({sym str} #Subjects.all) ...)
PATTERN

#on_send(node) ⇒ Object



33
34
35
36
37
38
39
40
41
42
# File 'lib/rubocop/cop/rspec/subject_declaration.rb', line 33

def on_send(node)
  offense = offensive_subject_declaration?(node)
  return unless offense

  add_offense(node, message: message_for(offense)) do |corrector|
    corrector.replace(node.loc.selector,
                      node.method_name.to_s.sub('let', 'subject'))
    corrector.remove(first_argument_range(node))
  end
end