Sha256: 9682e3c2c9ee7d8d96aad6b8a331b72eeb4b2329698d0598e0133d6e05a44138

Contents?: true

Size: 1.18 KB

Versions: 12

Compression:

Stored size: 1.18 KB

Contents

# frozen_string_literal: true

module RuboCop
  module Cop
    module RSpec
      # Prefer using verifying doubles over normal doubles.
      #
      # @see https://relishapp.com/rspec/rspec-mocks/docs/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 < Cop
        MSG = 'Prefer using verifying doubles over normal doubles.'

        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

12 entries across 12 versions & 1 rubygems

Version Path
rubocop-rspec-1.42.0 lib/rubocop/cop/rspec/verified_doubles.rb
rubocop-rspec-1.41.0 lib/rubocop/cop/rspec/verified_doubles.rb
rubocop-rspec-1.40.0 lib/rubocop/cop/rspec/verified_doubles.rb
rubocop-rspec-1.39.0 lib/rubocop/cop/rspec/verified_doubles.rb
rubocop-rspec-1.38.1 lib/rubocop/cop/rspec/verified_doubles.rb
rubocop-rspec-1.38.0 lib/rubocop/cop/rspec/verified_doubles.rb
rubocop-rspec-1.37.1 lib/rubocop/cop/rspec/verified_doubles.rb
rubocop-rspec-1.37.0 lib/rubocop/cop/rspec/verified_doubles.rb
rubocop-rspec-1.36.0 lib/rubocop/cop/rspec/verified_doubles.rb
rubocop-rspec-1.35.0 lib/rubocop/cop/rspec/verified_doubles.rb
rubocop-rspec-1.34.1 lib/rubocop/cop/rspec/verified_doubles.rb
rubocop-rspec-1.34.0 lib/rubocop/cop/rspec/verified_doubles.rb