Class: RuboCop::Cop::RSpecRails::InferredSpecType

Inherits:
RuboCop::Cop::RSpec::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
# RSpecRails/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

Instance Method Details

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

Parameters:

  • node (RuboCop::AST::BlockNode)


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

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