class RuboCop::Cop::RSpec::Rails::InferredSpecType
end
RSpec.describe User, type: :common do
# spec/services/user_spec.rb
# good
end
RSpec.describe User do
# spec/services/user_spec.rb
# good
end
RSpec.describe User, type: :service do
# spec/services/user_spec.rb
# bad
# services: service
# Inferences:
# RSpec/Rails/InferredSpecType:
# .rubocop.yml
@example ‘Inferences` configuration
end
RSpec.describe User, type: :common do
# spec/models/user_spec.rb
# good
end
RSpec.describe User do
# spec/models/user_spec.rb
# good
end
RSpec.describe User, type: :model do
# spec/models/user_spec.rb
# bad
@example
`config.infer_spec_type_from_file_location!` may not be enabled.
This cop is marked as unsafe because
@safety
If you disable this config, disable this cop as well.
spec/rails_helper.rb. This cop works in conjunction with this config.
`config.infer_spec_type_from_file_location!` by default in
After setting up rspec-rails, you will have enabled
Identifies redundant spec type.
def autocorrect(corrector, node)
-
node
(RuboCop::AST::Node
) -- -
corrector
(RuboCop::AST::Corrector
) --
def autocorrect(corrector, node) corrector.remove(remove_range(node)) end
def detect_removable_node(node)
-
(RuboCop::AST::Node)
-
Parameters:
-
node
(RuboCop::AST::PairNode
) --
def detect_removable_node(node) if node.parent.pairs.size == 1 node.parent else node end end
def file_path
-
(String)
-
def file_path processed_source.file_path end
def inferences
-
(Hash)
-
def inferences cop_config['Inferences'] || {} end
def inferred_type?(node)
-
(Boolean)
-
Parameters:
-
node
(RuboCop::AST::PairNode
) --
def inferred_type?(node) inferred_type_from_file_path.inspect == node.value.source end
def inferred_type_from_file_path
-
(Symbol, nil)
-
def inferred_type_from_file_path inferences.find do |prefix, type| break type.to_sym if file_path.include?("spec/#{prefix}/") end end
def on_block(node)
-
node
(RuboCop::AST::BlockNode
) --
def on_block(node) return unless example_group?(node) pair_node = describe_with_type(node) return unless pair_node return unless inferred_type?(pair_node) removable_node = detect_removable_node(pair_node) add_offense(removable_node) do |corrector| autocorrect(corrector, removable_node) end end
def remove_range(node)
-
(Parser::Source::Range)
-
Parameters:
-
node
(RuboCop::AST::Node
) --
def remove_range(node) if node.left_sibling node.source_range.with( begin_pos: node.left_sibling.source_range.end_pos ) elsif node.right_sibling node.source_range.with( end_pos: node.right_sibling.source_range.begin_pos ) end end