Sha256: 902572eba88dd3ce7f091e1e45511649a2b6ab5571d9909ca6a589766895b111

Contents?: true

Size: 1.73 KB

Versions: 7

Compression:

Stored size: 1.73 KB

Contents

# frozen_string_literal: true

module RuboCop
  module Cop
    module RSpec
      # Checks for explicitly referenced test subjects.
      #
      # RSpec lets you declare an "implicit subject" using `subject { ... }`
      # which allows for tests like `it { should be_valid }`. If you need to
      # reference your test subject you should explicitly name it using
      # `subject(:your_subject_name) { ... }`. Your test subjects should be
      # the most important object in your tests so they deserve a descriptive
      # name.
      #
      # @example
      #   # bad
      #   RSpec.describe User do
      #     subject { described_class.new }
      #
      #     it 'is valid' do
      #       expect(subject.valid?).to be(true)
      #     end
      #   end
      #
      #   # good
      #   RSpec.describe Foo do
      #     subject(:user) { described_class.new }
      #
      #     it 'is valid' do
      #       expect(user.valid?).to be(true)
      #     end
      #   end
      #
      #   # also good
      #   RSpec.describe Foo do
      #     subject(:user) { described_class.new }
      #
      #     it { should be_valid }
      #   end
      class NamedSubject < Cop
        MSG = 'Name your test subject if you need '\
              'to reference it explicitly.'.freeze

        def_node_matcher :rspec_block?, <<-PATTERN
          {
            #{Examples::ALL.block_pattern}
            #{Hooks::ALL.block_pattern}
          }
        PATTERN

        def_node_search :subject_usage, '$(send nil? :subject)'

        def on_block(node)
          return unless rspec_block?(node)

          subject_usage(node) do |subject_node|
            add_offense(subject_node, location: :selector)
          end
        end
      end
    end
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
rubocop-rspec-1.30.1 lib/rubocop/cop/rspec/named_subject.rb
rubocop-rspec-1.30.0 lib/rubocop/cop/rspec/named_subject.rb
rubocop-rspec-1.29.1 lib/rubocop/cop/rspec/named_subject.rb
rubocop-rspec-1.29.0 lib/rubocop/cop/rspec/named_subject.rb
rubocop-rspec-1.28.0 lib/rubocop/cop/rspec/named_subject.rb
rubocop-rspec-1.27.0 lib/rubocop/cop/rspec/named_subject.rb
rubocop-rspec-1.26.0 lib/rubocop/cop/rspec/named_subject.rb