Skip to content

Commit

Permalink
Add an option to allow enforcement of verified doubles in nameless do…
Browse files Browse the repository at this point in the history
…ubles

Currently if you have a double/spy with no name (or default stubs) it will never cause a violation according to the `RSpec/VerifiedDoubles` cop. This adds an optional configuration to allow violations to be added for code that looks like:

```
foo = double
allow(foo).to receive(:bar) do |arg1|
  “#{arg1} based result”
end
```
  • Loading branch information
BrentWheeldon committed Oct 3, 2018
1 parent 4ab2e3f commit 5edd6cb
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 5 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Expand Up @@ -2,6 +2,7 @@

## Master (Unreleased)

* Add config to `RSpec/VerifiedDoubles` to enforcement of verification on unnamed doubles. ([@BrentWheeldon][])
* Fix `FactoryBot/AttributeDefinedStatically` not working when there is a non-symbol key. ([@vzvu3k6k][])
* Fix false positive in `RSpec/ImplicitSubject` when `is_expected` is used inside `its()` block. ([@Darhazer][])
* Add `single_statement_only` style to `RSpec/ImplicitSubject` as a more relaxed alternative to `single_line_only`. ([@Darhazer][])
Expand Down Expand Up @@ -379,3 +380,4 @@ Compatibility release so users can upgrade RuboCop to 0.51.0. No new features.
[@composerinteralia]: https://github.com/composerinteralia
[@seanpdoyle]: https://github.com/seanpdoyle
[@vzvu3k6k]: https://github.com/vzvu3k6k
[@BrentWheeldon]: https://github.com/BrentWheeldon
1 change: 1 addition & 0 deletions config/default.yml
Expand Up @@ -402,6 +402,7 @@ RSpec/PredicateMatcher:
RSpec/VerifiedDoubles:
Description: Prefer using verifying doubles over normal doubles.
Enabled: true
IgnoreNameless: true
IgnoreSymbolicNames: false
StyleGuide: http://www.rubydoc.info/gems/rubocop-rspec/RuboCop/Cop/RSpec/VerifiedDoubles

Expand Down
13 changes: 10 additions & 3 deletions lib/rubocop/cop/rspec/verified_doubles.rb
Expand Up @@ -26,16 +26,23 @@ class VerifiedDoubles < Cop
MSG = 'Prefer using verifying doubles over normal doubles.'.freeze

def_node_matcher :unverified_double, <<-PATTERN
{(send nil? {:double :spy} $_ ...) }
{(send nil? {:double :spy} $...)}
PATTERN

def on_send(node)
unverified_double(node) do |name|
return if name.sym_type? && cop_config['IgnoreSymbolicNames']
unverified_double(node) do |name, *_args|
return if name.nil? && cop_config['IgnoreNameless']
return if symbol?(name) && cop_config['IgnoreSymbolicNames']

add_offense(node, location: :expression)
end
end

private

def symbol?(name)
name && name.sym_type?
end
end
end
end
Expand Down
1 change: 1 addition & 0 deletions manual/cops_rspec.md
Expand Up @@ -2494,6 +2494,7 @@ end

Name | Default value | Configurable values
--- | --- | ---
IgnoreNameless | `true` | Boolean
IgnoreSymbolicNames | `false` | Boolean

### References
Expand Down
17 changes: 15 additions & 2 deletions spec/rubocop/cop/rspec/verified_doubles_spec.rb
Expand Up @@ -53,14 +53,27 @@
end
end

it 'ignores doubles without a name' do
expect_no_offenses(<<-RUBY)
it 'doubles that have no name specified' do
expect_offense(<<-RUBY)
it do
foo = double
^^^^^^ Prefer using verifying doubles over normal doubles.
end
RUBY
end

context 'when configured to ignore nameless doubles' do
let(:cop_config) { { 'IgnoreNameless' => true } }

it 'ignores doubles that have no name specified' do
expect_no_offenses(<<-RUBY)
it do
foo = double
end
RUBY
end
end

it 'ignores instance_doubles' do
expect_no_offenses(<<-RUBY)
it do
Expand Down

0 comments on commit 5edd6cb

Please sign in to comment.