forked from rubocop/rubocop-rspec
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathany_instance.rb
45 lines (41 loc) · 1.19 KB
/
any_instance.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
42
43
44
45
# encoding: utf-8
module RuboCop
module Cop
module RSpec
# Pefer instance doubles over stubbing any instance of a class
#
# @example
# # bad
# describe MyClass do
# before { allow_any_instance_of(MyClass).to receive(:foo) }
# end
#
# # good
# describe MyClass do
# let(:my_instance) { instance_double(MyClass) }
#
# before do
# allow(MyClass).to receive(:new).and_return(my_instance)
# allow(my_instance).to receive(:foo)
# end
# end
class AnyInstance < Cop
MESSAGE = 'Avoid stubbing using `%{method}`'.freeze
ANY_INSTANCE_METHODS = [
:any_instance,
:allow_any_instance_of,
:expect_any_instance_of
].freeze
def on_send(node)
_receiver, method_name, *_args = *node
return unless ANY_INSTANCE_METHODS.include?(method_name)
add_offense(node, :expression,
format(MESSAGE % { method: method_name },
node.loc.expression.source
)
)
end
end
end
end
end