Class: RuboCop::Cop::RSpec::ImplicitExpect

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

Overview

Check that a consistent implicit expectation style is used.

This cop can be configured using the `EnforcedStyle` option and supports the `–auto-gen-config` flag.

Examples:

`EnforcedStyle: is_expected`


# bad
it { should be_truthy }

# good
it { is_expected.to be_truthy }

`EnforcedStyle: should`


# bad
it { is_expected.to be_truthy }

# good
it { should be_truthy }

Constant Summary collapse

MSG =
'Prefer `%<good>s` over `%<bad>s`.'
ENFORCED_REPLACEMENTS =
alternatives.merge(alternatives.invert).freeze

Instance Method Summary collapse

Methods inherited from Base

inherited, #on_new_investigation

Methods included from RSpec::Language::NodePattern

#block_pattern, #send_pattern

Instance Method Details

#on_send(node) ⇒ Object

rubocop:disable Metrics/MethodLength


48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/rubocop/cop/rspec/implicit_expect.rb', line 48

def on_send(node) # rubocop:disable Metrics/MethodLength
  return unless (source_range = offending_expect(node))

  expectation_source = source_range.source

  if expectation_source.start_with?(style.to_s)
    correct_style_detected
  else
    opposite_style_detected

    msg = offense_message(expectation_source)
    add_offense(source_range, message: msg) do |corrector|
      replacement = replacement_source(expectation_source)
      corrector.replace(source_range, replacement)
    end
  end
end