Sha256: 9d7f2526db252472b6627cdafa4be4eac3f9afd8dbcbc54b270783e740691f84

Contents?: true

Size: 1.28 KB

Versions: 23

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://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 < 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

23 entries across 21 versions & 4 rubygems

Version Path
scrapbook-0.3.2 vendor/ruby/2.7.0/gems/rubocop-rspec-2.13.1/lib/rubocop/cop/rspec/verified_doubles.rb
scrapbook-0.3.2 vendor/ruby/2.7.0/gems/rubocop-rspec-2.22.0/lib/rubocop/cop/rspec/verified_doubles.rb
mlh-rubocop-config-1.0.2 vendor/bundle/ruby/3.2.0/gems/rubocop-rspec-2.22.0/lib/rubocop/cop/rspec/verified_doubles.rb
fablicop-1.10.3 vendor/bundle/ruby/3.2.0/gems/rubocop-rspec-2.22.0/lib/rubocop/cop/rspec/verified_doubles.rb
scrapbook-0.3.1 vendor/ruby/2.7.0/gems/rubocop-rspec-2.13.1/lib/rubocop/cop/rspec/verified_doubles.rb
scrapbook-0.3.1 vendor/ruby/2.7.0/gems/rubocop-rspec-2.22.0/lib/rubocop/cop/rspec/verified_doubles.rb
fablicop-1.10.2 vendor/bundle/ruby/3.2.0/gems/rubocop-rspec-2.22.0/lib/rubocop/cop/rspec/verified_doubles.rb
rubocop-rspec-2.22.0 lib/rubocop/cop/rspec/verified_doubles.rb
rubocop-rspec-2.21.0 lib/rubocop/cop/rspec/verified_doubles.rb
rubocop-rspec-2.20.0 lib/rubocop/cop/rspec/verified_doubles.rb
rubocop-rspec-2.19.0 lib/rubocop/cop/rspec/verified_doubles.rb
rubocop-rspec-2.18.1 lib/rubocop/cop/rspec/verified_doubles.rb
rubocop-rspec-2.18.0 lib/rubocop/cop/rspec/verified_doubles.rb
rubocop-rspec-2.17.1 lib/rubocop/cop/rspec/verified_doubles.rb
rubocop-rspec-2.17.0 lib/rubocop/cop/rspec/verified_doubles.rb
rubocop-rspec-2.16.0 lib/rubocop/cop/rspec/verified_doubles.rb
rubocop-rspec-2.15.0 lib/rubocop/cop/rspec/verified_doubles.rb
rubocop-rspec-2.14.2 lib/rubocop/cop/rspec/verified_doubles.rb
rubocop-rspec-2.14.1 lib/rubocop/cop/rspec/verified_doubles.rb
rubocop-rspec-2.14.0 lib/rubocop/cop/rspec/verified_doubles.rb