class RuboCop::Cop::RSpec::NamedSubject

end
it { is_expected.to be_valid }
subject(:user) { described_class.new }
RSpec.describe Foo do
# also good
end
end
expect(user.valid?).to be(true)
it ‘is valid’ do
subject(:user) { described_class.new }
RSpec.describe Foo do
# good
end
end
expect(subject.valid?).to be(true)
it ‘is valid’ do
subject { described_class.new }
RSpec.describe User do
# bad
@example
subjects in shared example groups.
‘IgnoreSharedExamples` which will not report offenses for implicit
This cop can be configured in your configuration using the
a descriptive name.
should be the most important object in your tests so they deserve
name it using `subject(:your_subject_name) { … }`. Your test subjects
If you need to reference your test subject you should explicitly
which allows for tests like `it { is_expected.to be_valid }`.
RSpec lets you declare an “implicit subject” using `subject { … }`
Checks for explicitly referenced test subjects.

def ignored_shared_example?(node)

def ignored_shared_example?(node)
  cop_config['IgnoreSharedExamples'] &&
    node.each_ancestor(:block).any?(&method(:shared_example?))
end

def on_block(node)

def on_block(node)
  return if !rspec_block?(node) || ignored_shared_example?(node)
  subject_usage(node) do |subject_node|
    add_offense(subject_node, location: :selector)
  end
end