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

Inherits:
Base
  • Object
show all
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::NodePattern

#block_pattern, #send_pattern

Methods included from RSpec::Language

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

Instance Method Details

#offensive_subject_declaration?(node) ⇒ Object


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

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

#on_send(node) ⇒ Object


32
33
34
35
36
37
# File 'lib/rubocop/cop/rspec/subject_declaration.rb', line 32

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

  add_offense(node, message: message_for(offense))
end