Sha256: 67980f26bf5200ac88ab475f3450b18cc11604b22e95e7dfce274e35be96a507

Contents?: true

Size: 1.58 KB

Versions: 2

Compression:

Stored size: 1.58 KB

Contents

# frozen_string_literal: true

module RuboCop
  module Cop
    module RSpec
      # Give `subject` a descriptive name if you reference it directly
      #
      # @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
          (block
            (send nil {:it :specify :before :after :around} ...)
            ...)
        PATTERN

        def_node_matcher :unnamed_subject, '$(send nil :subject)'

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

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

        private

        def subject_usage(node, &block)
          return unless node.instance_of?(Node)

          unnamed_subject(node, &block)

          node.children.each do |child|
            subject_usage(child, &block)
          end
        end
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
rubocop-rspec-1.6.0 lib/rubocop/cop/rspec/named_subject.rb
rubocop-rspec-1.5.3 lib/rubocop/cop/rspec/named_subject.rb