Sha256: 43a0afedb10bc817e5a50f21244ef2e4500f368dc958cc9eae490dd4bd862493

Contents?: true

Size: 1.22 KB

Versions: 4

Compression:

Stored size: 1.22 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.'.freeze

        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, location: :expression)
          end
        end

        private

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

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
rubocop-rspec-1.32.0 lib/rubocop/cop/rspec/verified_doubles.rb
rubocop-rspec-1.31.0 lib/rubocop/cop/rspec/verified_doubles.rb
rubocop-rspec-1.30.1 lib/rubocop/cop/rspec/verified_doubles.rb
rubocop-rspec-1.30.0 lib/rubocop/cop/rspec/verified_doubles.rb