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

#find_arg_usage(node) ⇒ Object


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

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

#hook(node) ⇒ Object


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

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

#on_block(node) ⇒ Object


43
44
45
46
47
48
49
50
51
# File 'lib/rubocop/cop/rspec/around_block.rb', line 43

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