class RuboCop::Cop::RSpec::ImplicitSubject

Checks for usage of implicit subject (`is_expected` / `should`).

This cop can be configured using the `EnforcedStyle` option

@example `EnforcedStyle: single_line_only`

# bad
it do
  is_expected.to be_truthy
end

# good
it { is_expected.to be_truthy }
it do
  expect(subject).to be_truthy
end

@example `EnforcedStyle: disallow`

# bad
it { is_expected.to be_truthy }

# good
it { expect(subject).to be_truthy }

Constants

MSG

Public Instance Methods

autocorrect(node) click to toggle source
# File lib/rubocop/cop/rspec/implicit_subject.rb, line 45
def autocorrect(node)
  replacement = 'expect(subject)'
  if node.method_name == :should
    replacement += '.to'
  elsif node.method_name == :should_not
    replacement += '.not_to'
  end

  ->(corrector) { corrector.replace(node.loc.selector, replacement) }
end
on_send(node) click to toggle source
# File lib/rubocop/cop/rspec/implicit_subject.rb, line 38
def on_send(node)
  return unless implicit_subject?(node)
  return if valid_usage?(node)

  add_offense(node)
end

Private Instance Methods

allowed_by_style?(example) click to toggle source
# File lib/rubocop/cop/rspec/implicit_subject.rb, line 65
def allowed_by_style?(example)
  if style == :single_line_only
    example.single_line?
  elsif style == :single_statement_only
    !example.body.begin_type?
  else
    false
  end
end
valid_usage?(node) click to toggle source
# File lib/rubocop/cop/rspec/implicit_subject.rb, line 58
def valid_usage?(node)
  example = node.ancestors.find { |parent| example?(parent) }
  return false if example.nil?

  example.method_name == :its || allowed_by_style?(example)
end