Class: RuboCop::Cop::RSpec::AroundBlock

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

Overview

Checks that around blocks actually run the test.

Examples:

# bad
around do
  some_method
end

around do |test|
  some_method
end

# good
around do |test|
  some_method
  test.call
end

around do |test|
  some_method
  test.run
end

Constant Summary collapse

MSG_NO_ARG =
'Test object should be passed to around block.'
MSG_UNUSED_ARG =
'You should call `%<arg>s.call` ' \
'or `%<arg>s.run`.'

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

#find_arg_usage(node) ⇒ Object



45
46
47
# File 'lib/rubocop/cop/rspec/around_block.rb', line 45

def_node_search :find_arg_usage, <<~PATTERN
  {(send $... {:call :run}) (send _ _ $...) (yield $...) (block-pass $...)}
PATTERN

#hook_block(node) ⇒ Object



35
36
37
# File 'lib/rubocop/cop/rspec/around_block.rb', line 35

def_node_matcher :hook_block, <<~PATTERN
  (block (send nil? :around sym ?) (args $...) ...)
PATTERN

#hook_numblock(node) ⇒ Object



40
41
42
# File 'lib/rubocop/cop/rspec/around_block.rb', line 40

def_node_matcher :hook_numblock, <<~PATTERN
  (numblock (send nil? :around sym ?) ...)
PATTERN

#on_block(node) ⇒ Object



49
50
51
52
53
54
55
56
57
# File 'lib/rubocop/cop/rspec/around_block.rb', line 49

def on_block(node)
  hook_block(node) do |(example_proxy)|
    if example_proxy.nil?
      add_no_arg_offense(node)
    else
      check_for_unused_proxy(node, example_proxy)
    end
  end
end

#on_numblock(node) ⇒ Object



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

def on_numblock(node)
  hook_numblock(node) do
    check_for_numblock(node)
  end
end