Class: RuboCop::Cop::RSpec::DescribeMethod

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

Overview

Checks that the second argument to `describe` specifies a method.

Examples:

# bad
describe MyClass, 'do something' do
end

# good
describe MyClass, '#my_instance_method' do
end

describe MyClass, '.my_class_method' do
end

Constant Summary collapse

MSG =
'The second argument to describe should be the method ' \
"being tested. '#instance' or '.class'."

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_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

#on_top_level_group(node) ⇒ Object


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

def on_top_level_group(node)
  second_argument = second_argument(node)

  return unless second_argument
  return if second_argument.str_content.start_with?('#', '.')

  add_offense(second_argument)
end

#second_argument(node) ⇒ Object


26
27
28
29
30
# File 'lib/rubocop/cop/rspec/describe_method.rb', line 26

def_node_matcher :second_argument, <<~PATTERN
  (block
    (send #rspec? :describe _first_argument $(str _) ...) ...
  )
PATTERN