Class: RuboCop::Cop::RSpecRails::TravelAround

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

Overview

Prefer to travel in ‘before` rather than `around`.

Examples:

# bad
around do |example|
  freeze_time do
    example.run
  end
end

# good
before { freeze_time }

Constant Summary collapse

MSG =
'Prefer to travel in `before` rather than `around`.'
TRAVEL_METHOD_NAMES =
%i[
  freeze_time
  travel
  travel_to
].to_set.freeze

Instance Method Summary collapse

Instance Method Details

#extract_run_in_travel(node) ⇒ Object



38
39
40
41
42
43
44
# File 'lib/rubocop/cop/rspec_rails/travel_around.rb', line 38

def_node_matcher :extract_run_in_travel, <<~PATTERN
  (block
    $(send nil? TRAVEL_METHOD_NAMES ...)
    (args ...)
    (send _ :run)
  )
PATTERN

#match_around_each?(node) ⇒ Object



47
48
49
50
51
52
# File 'lib/rubocop/cop/rspec_rails/travel_around.rb', line 47

def_node_matcher :match_around_each?, <<~PATTERN
  (block
    (send _ :around (sym :each)?)
    ...
  )
PATTERN

#on_block(node) ⇒ Object Also known as: on_numblock



54
55
56
57
58
59
60
61
62
63
64
# File 'lib/rubocop/cop/rspec_rails/travel_around.rb', line 54

def on_block(node)
  run_node = extract_run_in_travel(node)
  return unless run_node

  around_node = extract_surrounding_around_block(run_node)
  return unless around_node

  add_offense(node) do |corrector|
    autocorrect(corrector, node, run_node, around_node)
  end
end