Sha256: 792767322cb85dbf38e127ffdafb9b5c0fef95823990ce400e71105c021f5b5b

Contents?: true

Size: 1.28 KB

Versions: 24

Compression:

Stored size: 1.28 KB

Contents

# frozen_string_literal: true

module RuboCop
  module Cop
    module RSpec
      # Prefer using verifying doubles over normal doubles.
      #
      # @see https://rspec.info/features/3-12/rspec-mocks/verifying-doubles
      #
      # @example
      #   # bad
      #   let(:foo) do
      #     double(method_name: 'returned value')
      #   end
      #
      #   # bad
      #   let(:foo) do
      #     double("ClassName", method_name: 'returned value')
      #   end
      #
      #   # good
      #   let(:foo) do
      #     instance_double("ClassName", method_name: 'returned value')
      #   end
      #
      class VerifiedDoubles < Base
        MSG = 'Prefer using verifying doubles over normal doubles.'
        RESTRICT_ON_SEND = %i[double spy].freeze

        # @!method unverified_double(node)
        def_node_matcher :unverified_double, <<~PATTERN
          {(send nil? {:double :spy} $...)}
        PATTERN

        def on_send(node)
          unverified_double(node) do |name, *_args|
            return if name.nil? && cop_config['IgnoreNameless']
            return if symbol?(name) && cop_config['IgnoreSymbolicNames']

            add_offense(node)
          end
        end

        private

        def symbol?(name)
          name&.sym_type?
        end
      end
    end
  end
end

Version data entries

24 entries across 24 versions & 5 rubygems

Version Path
minato_ruby_api_client-0.2.2 vendor/bundle/ruby/3.2.0/gems/rubocop-rspec-3.0.3/lib/rubocop/cop/rspec/verified_doubles.rb
rubocop-rspec-3.3.0 lib/rubocop/cop/rspec/verified_doubles.rb
rubocop-rspec-3.2.0 lib/rubocop/cop/rspec/verified_doubles.rb
rubocop-rspec-3.1.0 lib/rubocop/cop/rspec/verified_doubles.rb
rubocop-rspec-3.0.5 lib/rubocop/cop/rspec/verified_doubles.rb
rubocop-rspec-3.0.4 lib/rubocop/cop/rspec/verified_doubles.rb
blacklight-spotlight-3.6.0.beta8 vendor/bundle/ruby/3.2.0/gems/rubocop-rspec-3.0.1/lib/rubocop/cop/rspec/verified_doubles.rb
rubocop-rspec-3.0.3 lib/rubocop/cop/rspec/verified_doubles.rb
rubocop-rspec-3.0.2 lib/rubocop/cop/rspec/verified_doubles.rb
katalyst-govuk-formbuilder-1.9.2 vendor/bundle/ruby/3.3.0/gems/rubocop-rspec-3.0.1/lib/rubocop/cop/rspec/verified_doubles.rb
rubocop-rspec-3.0.1 lib/rubocop/cop/rspec/verified_doubles.rb
rubocop-rspec-3.0.0 lib/rubocop/cop/rspec/verified_doubles.rb
rubocop-rspec-3.0.0.pre lib/rubocop/cop/rspec/verified_doubles.rb
rubocop-rspec-2.31.0 lib/rubocop/cop/rspec/verified_doubles.rb
rubocop-rspec-2.30.0 lib/rubocop/cop/rspec/verified_doubles.rb
rubocop-rspec-2.29.2 lib/rubocop/cop/rspec/verified_doubles.rb
rubocop-rspec-2.29.1 lib/rubocop/cop/rspec/verified_doubles.rb
rubocop-rspec-2.29.0 lib/rubocop/cop/rspec/verified_doubles.rb
rubocop-rspec-2.28.0 lib/rubocop/cop/rspec/verified_doubles.rb
rubocop-rspec-2.27.1 lib/rubocop/cop/rspec/verified_doubles.rb