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

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

Instance Method Details

#method_name?(node) ⇒ Object



34
35
36
# File 'lib/rubocop/cop/rspec/describe_method.rb', line 34

def_node_matcher :method_name?, <<~PATTERN
  {(str #method_name_prefix?) (dstr (str #method_name_prefix?) ...)}
PATTERN

#on_top_level_group(node) ⇒ Object



38
39
40
41
42
# File 'lib/rubocop/cop/rspec/describe_method.rb', line 38

def on_top_level_group(node)
  second_string_literal_argument(node) do |argument|
    add_offense(argument) unless method_name?(argument)
  end
end

#second_string_literal_argument(node) ⇒ Object



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

def_node_matcher :second_string_literal_argument, <<~PATTERN
  (block
    (send #rspec? :describe _first_argument ${str dstr} ...)
  ...)
PATTERN