Class: RuboCop::Cop::RSpec::DescribeClass

Inherits:
Base
  • Object
show all
Includes:
TopLevelGroup
Defined in:
lib/rubocop/cop/rspec/describe_class.rb

Overview

Check that the first argument to the top-level describe is a constant.

It can be configured to ignore strings when certain metadata is passed.

Ignores Rails and Aruba ‘type` metadata by default.

Examples:

‘IgnoredMetadata` configuration

# .rubocop.yml
# RSpec/DescribeClass:
#   IgnoredMetadata:
#     type:
#       - request
#       - controller
# bad
describe 'Do something' do
end

# good
describe TestedClass do
  subject { described_class }
end

describe 'TestedClass::VERSION' do
  subject { Object.const_get(self.class.description) }
end

describe "A feature example", type: :feature do
end

Constant Summary collapse

MSG =
'The first argument to describe should be ' \
'the class or module being tested.'

Instance Method Summary collapse

Methods included from TopLevelGroup

#on_new_investigation, #top_level_groups

Methods inherited from Base

inherited, #on_new_investigation

Methods included from RSpec::Language::NodePattern

#block_or_numblock_pattern, #block_pattern, #numblock_pattern, #send_pattern

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

#example_group_with_ignored_metadata?(node) ⇒ Object



44
45
46
# File 'lib/rubocop/cop/rspec/describe_class.rb', line 44

def_node_matcher :example_group_with_ignored_metadata?, <<~PATTERN
  (send #rspec? :describe ... (hash <#ignored_metadata? ...>))
PATTERN

#not_a_const_described(node) ⇒ Object



49
50
51
# File 'lib/rubocop/cop/rspec/describe_class.rb', line 49

def_node_matcher :not_a_const_described, <<~PATTERN
  (send #rspec? :describe $[!const !#string_constant?] ...)
PATTERN

#on_top_level_group(node) ⇒ Object



58
59
60
61
62
63
64
# File 'lib/rubocop/cop/rspec/describe_class.rb', line 58

def on_top_level_group(node)
  return if example_group_with_ignored_metadata?(node.send_node)

  not_a_const_described(node.send_node) do |described|
    add_offense(described)
  end
end

#sym_pair(node) ⇒ Object



54
55
56
# File 'lib/rubocop/cop/rspec/describe_class.rb', line 54

def_node_matcher :sym_pair, <<~PATTERN
  (pair $sym $sym)
PATTERN