Class: RuboCop::Cop::RSpec::ContextMethod

Inherits:
Base
  • Object
show all
Extended by:
AutoCorrector
Defined in:
lib/rubocop/cop/rspec/context_method.rb

Overview

‘context` should not be used for specifying methods.

Examples:

# bad
context '#foo_bar' do
  # ...
end

context '.foo_bar' do
  # ...
end

# good
describe '#foo_bar' do
  # ...
end

describe '.foo_bar' do
  # ...
end

Constant Summary collapse

MSG =
'Use `describe` for testing methods.'

Instance Method Summary collapse

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

#context_method(node) ⇒ Object



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

def_node_matcher :context_method, <<~PATTERN
  (block
    (send #rspec? :context
      ${(str #method_name?) (dstr (str #method_name?) ...)}
    ...)
  ...)
PATTERN

#on_block(node) ⇒ Object

rubocop:disable InternalAffairs/NumblockHandler



41
42
43
44
45
46
47
# File 'lib/rubocop/cop/rspec/context_method.rb', line 41

def on_block(node) # rubocop:disable InternalAffairs/NumblockHandler
  context_method(node) do |context|
    add_offense(context) do |corrector|
      corrector.replace(node.send_node.loc.selector, 'describe')
    end
  end
end