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

# bad
around do |example|
  freeze_time(&example)
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

[View source]

44
45
46
47
48
49
50
# File 'lib/rubocop/cop/rspec_rails/travel_around.rb', line 44

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

#extract_travel_with_block_pass(node) ⇒ Object

[View source]

53
54
55
56
57
# File 'lib/rubocop/cop/rspec_rails/travel_around.rb', line 53

def_node_search :extract_travel_with_block_pass, <<~PATTERN
  $(send _ TRAVEL_METHOD_NAMES
    (block_pass $lvar)
  )
PATTERN

#match_around_each?(node) ⇒ Object

[View source]

60
61
62
63
64
65
# File 'lib/rubocop/cop/rspec_rails/travel_around.rb', line 60

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

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

[View source]

67
68
69
70
71
72
73
74
# File 'lib/rubocop/cop/rspec_rails/travel_around.rb', line 67

def on_block(node)
  extract_run_in_travel(node) do |run_node|
    run_in_travel(node, run_node)
  end
  extract_travel_with_block_pass(node) do |travel_node, lvar|
    travel_with_block_pass(travel_node, lvar)
  end
end