Class: RuboCop::Cop::RSpec::ClassCheck

Inherits:
Base
  • Object
show all
Extended by:
AutoCorrector
Includes:
ConfigurableEnforcedStyle
Defined in:
lib/rubocop/cop/rspec/class_check.rb

Overview

Enforces consistent use of ‘be_a` or `be_kind_of`.

Examples:

EnforcedStyle: be_a (default)

# bad
expect(object).to be_kind_of(String)
expect(object).to be_a_kind_of(String)

# good
expect(object).to be_a(String)
expect(object).to be_an(String)

EnforcedStyle: be_kind_of

# bad
expect(object).to be_a(String)
expect(object).to be_an(String)

# good
expect(object).to be_kind_of(String)
expect(object).to be_a_kind_of(String)

Constant Summary collapse

MSG =
'Prefer `%<preferred>s` over `%<current>s`.'
METHOD_NAMES_FOR_BE_A =
::Set[
  :be_a,
  :be_an
].freeze
METHOD_NAMES_FOR_KIND_OF =
::Set[
  :be_a_kind_of,
  :be_kind_of
].freeze
PREFERRED_METHOD_NAME_BY_STYLE =
{
  be_a: :be_a,
  be_kind_of: :be_kind_of
}.freeze
RESTRICT_ON_SEND =
%i[
  be_a
  be_a_kind_of
  be_an
  be_kind_of
].freeze

Instance Method Summary collapse

Methods inherited from Base

inherited, #on_new_investigation

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_send(node) ⇒ Object



54
55
56
57
58
59
60
61
62
63
# File 'lib/rubocop/cop/rspec/class_check.rb', line 54

def on_send(node)
  return unless offending?(node)

  add_offense(
    node.loc.selector,
    message: format_message(node)
  ) do |corrector|
    autocorrect(corrector, node)
  end
end