Class: RuboCop::Cop::RSpec::Rails::InferredSpecType

Inherits:
Base
  • Object
show all
Extended by:
AutoCorrector
Defined in:
lib/rubocop/cop/rspec/rails/inferred_spec_type.rb

Overview

Identifies redundant spec type.

After setting up rspec-rails, you will have enabled ‘config.infer_spec_type_from_file_location!` by default in spec/rails_helper.rb. This cop works in conjunction with this config. If you disable this config, disable this cop as well.

Examples:

# bad
# spec/models/user_spec.rb
RSpec.describe User, type: :model do
end

# good
# spec/models/user_spec.rb
RSpec.describe User do
end

# good
# spec/models/user_spec.rb
RSpec.describe User, type: :common do
end

‘Inferences` configuration

# .rubocop.yml
# RSpec/Rails/InferredSpecType:
#   Inferences:
#     services: service

# bad
# spec/services/user_spec.rb
RSpec.describe User, type: :service do
end

# good
# spec/services/user_spec.rb
RSpec.describe User do
end

# good
# spec/services/user_spec.rb
RSpec.describe User, type: :common do
end

Constant Summary collapse

MSG =
'Remove redundant spec type.'

Instance Method Summary collapse

Methods inherited from Base

inherited, #on_new_investigation

Methods included from RSpec::Language::NodePattern

#block_or_numblock_pattern, #block_pattern, #numblock_pattern, #send_pattern

Methods included from RSpec::Language

#example?, #example_group?, #example_group_with_body?, #explicit_rspec?, #hook?, #include?, #let?, #rspec?, #shared_group?, #spec_group?, #subject?

Instance Method Details

#on_block(node) ⇒ Object Also known as: on_numblock

Parameters:

  • node (RuboCop::AST::BlockNode)


60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/rubocop/cop/rspec/rails/inferred_spec_type.rb', line 60

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