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

Inherits:
Cop
  • Object
show all
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`.'.freeze
ENFORCED_REPLACEMENTS =
alternatives.merge(alternatives.invert).freeze

Constants inherited from Cop

Cop::DEFAULT_CONFIGURATION, Cop::DEFAULT_PATTERN_RE

Constants included from RSpec::Language

RSpec::Language::ALL

Instance Method Summary collapse

Methods inherited from Cop

inherited, #relevant_file?

Instance Method Details

#autocorrect(node) ⇒ Object



65
66
67
68
69
70
71
72
# File 'lib/rubocop/cop/rspec/implicit_expect.rb', line 65

def autocorrect(node)
  lambda do |corrector|
    offense     = offending_expect(node)
    replacement = replacement_source(offense.source)

    corrector.replace(offense, replacement)
  end
end

#on_send(node) ⇒ Object

rubocop:disable Metrics/MethodLength



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

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

    add_offense(
      node,
      location: source_range,
      message: offense_message(expectation_source)
    )
  end
end