Sha256: 4c6b308aa2a4ff323ebec13227c0628ad324f07e1b50f10830f3a9de7531b8b3

Contents?: true

Size: 1.89 KB

Versions: 29

Compression:

Stored size: 1.89 KB

Contents

# frozen_string_literal: true

module RuboCop
  module Cop
    module RSpec
      # Checks for `instance_double` used with `have_received`.
      #
      # @example
      #   # bad
      #   it do
      #     foo = instance_double(Foo).as_null_object
      #     expect(foo).to have_received(:bar)
      #   end
      #
      #   # good
      #   it do
      #     foo = instance_spy(Foo)
      #     expect(foo).to have_received(:bar)
      #   end
      #
      class InstanceSpy < Base
        extend AutoCorrector

        MSG = 'Use `instance_spy` when you check your double ' \
              'with `have_received`.'

        # @!method null_double(node)
        def_node_search :null_double, <<-PATTERN
          (lvasgn $_
            (send
              $(send nil? :instance_double
                ...) :as_null_object))
        PATTERN

        # @!method have_received_usage(node)
        def_node_search :have_received_usage, <<-PATTERN
          (send
            (send nil? :expect
            (lvar $_)) :to
            (send nil? :have_received
            ...)
          ...)
        PATTERN

        def on_block(node) # rubocop:disable InternalAffairs/NumblockHandler
          return unless example?(node)

          null_double(node) do |var, receiver|
            have_received_usage(node) do |expected|
              next if expected != var

              add_offense(receiver) do |corrector|
                autocorrect(corrector, receiver)
              end
            end
          end
        end

        private

        def autocorrect(corrector, node)
          replacement = 'instance_spy'
          corrector.replace(node.loc.selector, replacement)

          double_source_map = node.parent.loc
          as_null_object_range = double_source_map
            .dot
            .join(double_source_map.selector)
          corrector.remove(as_null_object_range)
        end
      end
    end
  end
end

Version data entries

29 entries across 27 versions & 4 rubygems

Version Path
scrapbook-0.3.2 vendor/ruby/2.7.0/gems/rubocop-rspec-2.13.1/lib/rubocop/cop/rspec/instance_spy.rb
scrapbook-0.3.2 vendor/ruby/2.7.0/gems/rubocop-rspec-2.22.0/lib/rubocop/cop/rspec/instance_spy.rb
rubocop-rspec-2.25.0 lib/rubocop/cop/rspec/instance_spy.rb
rubocop-rspec-2.24.1 lib/rubocop/cop/rspec/instance_spy.rb
rubocop-rspec-2.24.0 lib/rubocop/cop/rspec/instance_spy.rb
rubocop-rspec-2.23.2 lib/rubocop/cop/rspec/instance_spy.rb
rubocop-rspec-2.23.1 lib/rubocop/cop/rspec/instance_spy.rb
rubocop-rspec-2.23.0 lib/rubocop/cop/rspec/instance_spy.rb
mlh-rubocop-config-1.0.2 vendor/bundle/ruby/3.2.0/gems/rubocop-rspec-2.22.0/lib/rubocop/cop/rspec/instance_spy.rb
fablicop-1.10.3 vendor/bundle/ruby/3.2.0/gems/rubocop-rspec-2.22.0/lib/rubocop/cop/rspec/instance_spy.rb
scrapbook-0.3.1 vendor/ruby/2.7.0/gems/rubocop-rspec-2.13.1/lib/rubocop/cop/rspec/instance_spy.rb
scrapbook-0.3.1 vendor/ruby/2.7.0/gems/rubocop-rspec-2.22.0/lib/rubocop/cop/rspec/instance_spy.rb
fablicop-1.10.2 vendor/bundle/ruby/3.2.0/gems/rubocop-rspec-2.22.0/lib/rubocop/cop/rspec/instance_spy.rb
rubocop-rspec-2.22.0 lib/rubocop/cop/rspec/instance_spy.rb
rubocop-rspec-2.21.0 lib/rubocop/cop/rspec/instance_spy.rb
rubocop-rspec-2.20.0 lib/rubocop/cop/rspec/instance_spy.rb
rubocop-rspec-2.19.0 lib/rubocop/cop/rspec/instance_spy.rb
rubocop-rspec-2.18.1 lib/rubocop/cop/rspec/instance_spy.rb
rubocop-rspec-2.18.0 lib/rubocop/cop/rspec/instance_spy.rb
rubocop-rspec-2.17.1 lib/rubocop/cop/rspec/instance_spy.rb