-
Notifications
You must be signed in to change notification settings - Fork 0
/
avoid_setup_hook.rb
41 lines (37 loc) · 967 Bytes
/
avoid_setup_hook.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# frozen_string_literal: true
module RuboCop
module Cop
module RSpecRails
# Checks that tests use RSpec `before` hook over Rails `setup` method.
#
# @example
# # bad
# setup do
# allow(foo).to receive(:bar)
# end
#
# # good
# before do
# allow(foo).to receive(:bar)
# end
#
class AvoidSetupHook < ::RuboCop::Cop::RSpec::Base
extend AutoCorrector
MSG = 'Use `before` instead of `setup`.'
# @!method setup_call(node)
def_node_matcher :setup_call, <<~PATTERN
(block
$(send nil? :setup)
(args) _)
PATTERN
def on_block(node) # rubocop:disable InternalAffairs/NumblockHandler
setup_call(node) do |setup|
add_offense(node) do |corrector|
corrector.replace setup, 'before'
end
end
end
end
end
end
end