Class: RuboCop::Cop::RSpec::IsExpectedSpecify

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

Overview

Check for ‘specify` with `is_expected` and one-liner expectations.

Examples:

# bad
specify { is_expected.to be_truthy }

# good
it { is_expected.to be_truthy }

# good
specify do
  # ...
end
specify { expect(sqrt(4)).to eq(2) }

Constant Summary collapse

RESTRICT_ON_SEND =
%i[specify].freeze
IS_EXPECTED_METHODS =
::Set[:is_expected, :are_expected].freeze
MSG =
'Use `it` instead of `specify`.'

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

#offense?(node) ⇒ Object



29
30
31
# File 'lib/rubocop/cop/rspec/is_expected_specify.rb', line 29

def_node_matcher :offense?, <<~PATTERN
  (block (send _ :specify) _ (send (send _ IS_EXPECTED_METHODS) ...))
PATTERN

#on_send(node) ⇒ Object



33
34
35
36
37
38
39
40
41
# File 'lib/rubocop/cop/rspec/is_expected_specify.rb', line 33

def on_send(node)
  block_node = node.parent
  return unless block_node&.single_line? && offense?(block_node)

  selector = node.loc.selector
  add_offense(selector) do |corrector|
    corrector.replace(selector, 'it')
  end
end