-
-
Notifications
You must be signed in to change notification settings - Fork 276
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1957 from corsonknowles/master
Add RSpec/StringAsInstanceDoubleConstant
- Loading branch information
Showing
8 changed files
with
126 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
45 changes: 45 additions & 0 deletions
45
lib/rubocop/cop/rspec/string_as_instance_double_constant.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
# frozen_string_literal: true | ||
|
||
module RuboCop | ||
module Cop | ||
module RSpec | ||
# Do not use a string as `instance_double` constant. | ||
# | ||
# @safety | ||
# This cop is unsafe because the correction requires loading the class. | ||
# Loading before stubbing causes RSpec to only allow instance methods | ||
# to be stubbed. | ||
# | ||
# @example | ||
# # bad | ||
# instance_double('User', name: 'John') | ||
# | ||
# # good | ||
# instance_double(User, name: 'John') | ||
# | ||
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
33 changes: 33 additions & 0 deletions
33
spec/rubocop/cop/rspec/string_as_instance_double_constant_spec.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 'does not register an offense' do | ||
expect_no_offenses(<<~RUBY) | ||
instance_double(Shape, area: 12) | ||
RUBY | ||
end | ||
end | ||
|
||
context 'when passing a variable to initialize instance_double' do | ||
it 'does not register an offense' do | ||
expect_no_offenses(<<~RUBY) | ||
instance_double(type_undetectable_in_static_analysis) | ||
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('Shape', area: 12) | ||
^^^^^^^ Do not use a string as `instance_double` constant. | ||
RUBY | ||
|
||
expect_correction <<~RUBY | ||
instance_double(Shape, area: 12) | ||
RUBY | ||
end | ||
end | ||
end |