forked from rubocop/rubocop-rspec
-
Notifications
You must be signed in to change notification settings - Fork 0
/
verified_doubles.rb
49 lines (43 loc) · 1.18 KB
/
verified_doubles.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
46
47
48
49
# frozen_string_literal: true
module RuboCop
module Cop
module RSpec
# Prefer using verifying doubles over normal doubles.
#
# @see https://relishapp.com/rspec/rspec-mocks/docs/verifying-doubles
#
# @example
# # bad
# let(:foo) do
# double(method_name: 'returned value')
# end
#
# # bad
# let(:foo) do
# double("ClassName", method_name: 'returned value')
# end
#
# # good
# let(:foo) do
# instance_double("ClassName", method_name: 'returned value')
# end
class VerifiedDoubles < Cop
MSG = 'Prefer using verifying doubles over normal doubles.'
def_node_matcher :unverified_double, <<-PATTERN
{(send nil? {:double :spy} $...)}
PATTERN
def on_send(node)
unverified_double(node) do |name, *_args|
return if name.nil? && cop_config['IgnoreNameless']
return if symbol?(name) && cop_config['IgnoreSymbolicNames']
add_offense(node)
end
end
private
def symbol?(name)
name&.sym_type?
end
end
end
end
end