Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 507a7f7

Browse files
author
Maxim Krizhanovsky
authored
Merge pull request rubocop#691 from BrentWheeldon/bmw-verify-nameless-doubles
Add an option to allow enforcement of verified doubles in nameless doubles
2 parents ccf04ec + d118fba commit 507a7f7

File tree

5 files changed

+29
-5
lines changed

5 files changed

+29
-5
lines changed

CHANGELOG.md

+2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
## Master (Unreleased)
44

5+
* Add config to `RSpec/VerifiedDoubles` to enforcement of verification on unnamed doubles. ([@BrentWheeldon][])
56
* Fix `FactoryBot/AttributeDefinedStatically` not working when there is a non-symbol key. ([@vzvu3k6k][])
67
* Fix false positive in `RSpec/ImplicitSubject` when `is_expected` is used inside `its()` block. ([@Darhazer][])
78
* Add `single_statement_only` style to `RSpec/ImplicitSubject` as a more relaxed alternative to `single_line_only`. ([@Darhazer][])
@@ -379,3 +380,4 @@ Compatibility release so users can upgrade RuboCop to 0.51.0. No new features.
379380
[@composerinteralia]: https://github.com/composerinteralia
380381
[@seanpdoyle]: https://github.com/seanpdoyle
381382
[@vzvu3k6k]: https://github.com/vzvu3k6k
383+
[@BrentWheeldon]: https://github.com/BrentWheeldon

config/default.yml

+1
Original file line numberDiff line numberDiff line change
@@ -402,6 +402,7 @@ RSpec/SubjectStub:
402402
RSpec/VerifiedDoubles:
403403
Description: Prefer using verifying doubles over normal doubles.
404404
Enabled: true
405+
IgnoreNameless: true
405406
IgnoreSymbolicNames: false
406407
StyleGuide: http://www.rubydoc.info/gems/rubocop-rspec/RuboCop/Cop/RSpec/VerifiedDoubles
407408

lib/rubocop/cop/rspec/verified_doubles.rb

+10-3
Original file line numberDiff line numberDiff line change
@@ -26,16 +26,23 @@ class VerifiedDoubles < Cop
2626
MSG = 'Prefer using verifying doubles over normal doubles.'.freeze
2727

2828
def_node_matcher :unverified_double, <<-PATTERN
29-
{(send nil? {:double :spy} $_ ...) }
29+
{(send nil? {:double :spy} $...)}
3030
PATTERN
3131

3232
def on_send(node)
33-
unverified_double(node) do |name|
34-
return if name.sym_type? && cop_config['IgnoreSymbolicNames']
33+
unverified_double(node) do |name, *_args|
34+
return if name.nil? && cop_config['IgnoreNameless']
35+
return if symbol?(name) && cop_config['IgnoreSymbolicNames']
3536

3637
add_offense(node, location: :expression)
3738
end
3839
end
40+
41+
private
42+
43+
def symbol?(name)
44+
name && name.sym_type?
45+
end
3946
end
4047
end
4148
end

manual/cops_rspec.md

+1
Original file line numberDiff line numberDiff line change
@@ -2494,6 +2494,7 @@ end
24942494

24952495
Name | Default value | Configurable values
24962496
--- | --- | ---
2497+
IgnoreNameless | `true` | Boolean
24972498
IgnoreSymbolicNames | `false` | Boolean
24982499

24992500
### References

spec/rubocop/cop/rspec/verified_doubles_spec.rb

+15-2
Original file line numberDiff line numberDiff line change
@@ -53,14 +53,27 @@
5353
end
5454
end
5555

56-
it 'ignores doubles without a name' do
57-
expect_no_offenses(<<-RUBY)
56+
it 'doubles that have no name specified' do
57+
expect_offense(<<-RUBY)
5858
it do
5959
foo = double
60+
^^^^^^ Prefer using verifying doubles over normal doubles.
6061
end
6162
RUBY
6263
end
6364

65+
context 'when configured to ignore nameless doubles' do
66+
let(:cop_config) { { 'IgnoreNameless' => true } }
67+
68+
it 'ignores doubles that have no name specified' do
69+
expect_no_offenses(<<-RUBY)
70+
it do
71+
foo = double
72+
end
73+
RUBY
74+
end
75+
end
76+
6477
it 'ignores instance_doubles' do
6578
expect_no_offenses(<<-RUBY)
6679
it do

0 commit comments

Comments
 (0)