Skip to content

Commit

Permalink
Add RSpec/StringAsInstanceDoubleConstant
Browse files Browse the repository at this point in the history
Addresses rubocop#1136

Adds a cop which can autocorrect from String declarations for instance_double to Class declarations. Symbol declarations are not affected.
  • Loading branch information
corsonknowles committed Sep 18, 2024
1 parent 16cf19c commit 7c794c1
Show file tree
Hide file tree
Showing 8 changed files with 116 additions and 1 deletion.
3 changes: 2 additions & 1 deletion .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -253,4 +253,5 @@ Style/YAMLFileRead: {Enabled: true}

# Enable our own pending cops.
#
# No pending cops yet.
RSpec/StringAsInstanceDoubleConstant:
Enabled: true
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

## Master (Unreleased)

- Add `RSpec/StringAsInstanceDoubleConstant` to check for and correct strings used as instance_doubles. ([@corsonknowles])

## 3.0.5 (2024-09-07)

- Fix false-negative and error for `RSpec/MetadataStyle` when non-literal args are used in metadata in `EnforceStyle: hash`. ([@cbliard])
Expand Down Expand Up @@ -920,6 +922,7 @@ Compatibility release so users can upgrade RuboCop to 0.51.0. No new features.
[@cfabianski]: https://github.com/cfabianski
[@clupprich]: https://github.com/clupprich
[@composerinteralia]: https://github.com/composerinteralia
[@corsonknowles]: https://github.com/corsonknowles
[@corydiamand]: https://github.com/corydiamand
[@darhazer]: https://github.com/Darhazer
[@daveworth]: https://github.com/daveworth
Expand Down
7 changes: 7 additions & 0 deletions config/default.yml
Original file line number Diff line number Diff line change
Expand Up @@ -929,6 +929,13 @@ RSpec/SpecFilePathSuffix:
- "**/spec/**/*"
Reference: https://www.rubydoc.info/gems/rubocop-rspec/RuboCop/Cop/RSpec/SpecFilePathSuffix

RSpec/StringAsInstanceDoubleConstant:
Description: Do not use a string as `instance_double` constant.
Enabled: pending
Safe: false
VersionAdded: "<<next>>"
Reference: https://www.rubydoc.info/gems/rubocop-rspec/RuboCop/Cop/RSpec/StringAsInstanceDoubleConstant

RSpec/StubbedMock:
Description: Checks that message expectations do not have a configured response.
Enabled: true
Expand Down
1 change: 1 addition & 0 deletions docs/modules/ROOT/pages/cops.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@
* xref:cops_rspec.adoc#rspecsortmetadata[RSpec/SortMetadata]
* xref:cops_rspec.adoc#rspecspecfilepathformat[RSpec/SpecFilePathFormat]
* xref:cops_rspec.adoc#rspecspecfilepathsuffix[RSpec/SpecFilePathSuffix]
* xref:cops_rspec.adoc#rspecstringasinstancedoubleconstant[RSpec/StringAsInstanceDoubleConstant]
* xref:cops_rspec.adoc#rspecstubbedmock[RSpec/StubbedMock]
* xref:cops_rspec.adoc#rspecsubjectdeclaration[RSpec/SubjectDeclaration]
* xref:cops_rspec.adoc#rspecsubjectstub[RSpec/SubjectStub]
Expand Down
29 changes: 29 additions & 0 deletions docs/modules/ROOT/pages/cops_rspec.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -5493,6 +5493,35 @@ spec/models/user.rb # shared_examples_for 'foo'
* https://www.rubydoc.info/gems/rubocop-rspec/RuboCop/Cop/RSpec/SpecFilePathSuffix
== RSpec/StringAsInstanceDoubleConstant
|===
| Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed
| Pending
| No
| Always (Unsafe)
| <<next>>
| -
|===
Do not use a string as `instance_double` constant.
=== Examples
[source,ruby]
----
# bad
instance_double('Foo', failure_message, stubs)
# good
instance_double(Foo, failure_message, stubs)
----
=== References
* https://www.rubydoc.info/gems/rubocop-rspec/RuboCop/Cop/RSpec/StringAsInstanceDoubleConstant
== RSpec/StubbedMock
|===
Expand Down
40 changes: 40 additions & 0 deletions lib/rubocop/cop/rspec/string_as_instance_double_constant.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
# frozen_string_literal: true

module RuboCop
module Cop
module RSpec
# Do not use a string as `instance_double` constant.
#
# @example
# # bad
# instance_double('Foo', failure_message, stubs)
#
# # good
# instance_double(Foo, failure_message, stubs)
#
class StringAsInstanceDoubleConstant < Base
extend AutoCorrector

MSG = 'Do not use a string as `instance_double` constant.'
RESTRICT_ON_SEND = %i[instance_double].freeze

# @!method stringified_instance_double_const?(node)
def_node_matcher :stringified_instance_double_const?, <<~PATTERN
(send nil? :instance_double $str ...)
PATTERN

def on_send(node)
stringified_instance_double_const?(node) do |args_node|
add_offense(args_node) do |corrector|
autocorrect(corrector, args_node)
end
end
end

def autocorrect(corrector, node)
corrector.replace(node, node.value)
end
end
end
end
end
1 change: 1 addition & 0 deletions lib/rubocop/cop/rspec_cops.rb
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@
require_relative 'rspec/sort_metadata'
require_relative 'rspec/spec_file_path_format'
require_relative 'rspec/spec_file_path_suffix'
require_relative 'rspec/string_as_instance_double_constant'
require_relative 'rspec/stubbed_mock'
require_relative 'rspec/subject_declaration'
require_relative 'rspec/subject_stub'
Expand Down
33 changes: 33 additions & 0 deletions spec/rubocop/cop/rspec/string_as_instance_double_constant_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# frozen_string_literal: true

RSpec.describe RuboCop::Cop::RSpec::StringAsInstanceDoubleConstant,
:config do
context 'when using a class for instance_double' do
it do
expect_no_offenses(<<~RUBY)
instance_double(Foo, failure_message, stubs)
RUBY
end
end

context 'when using a symbol for instance_double' do
it do
expect_no_offenses(<<~RUBY)
instance_double(:Foo, failure_message, stubs)
RUBY
end
end

context 'when using a string for instance_double' do
it 'replaces the string with the class' do
expect_offense <<~RUBY
instance_double('Foo', failure_message, stubs)
^^^^^ Do not use a string as `instance_double` constant.
RUBY

expect_correction <<~RUBY
instance_double(Foo, failure_message, stubs)
RUBY
end
end
end

0 comments on commit 7c794c1

Please sign in to comment.