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

#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



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

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

#on_send(node) ⇒ Object



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

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

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