Sha256: 7cec6c1b0eafb5da4f0da797ee8cd1e182754497f4a029e0c3513c8bae7aadbc

Contents?: true

Size: 1.81 KB

Versions: 8

Compression:

Stored size: 1.81 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 < Cop
        MSG = 'Use `instance_spy` when you check your double '\
              'with `have_received`.'.freeze

        EXAMPLES = Examples::ALL.node_pattern_union.freeze

        def_node_matcher :example?, "(block $(send nil #{EXAMPLES}) ...)"

        def_node_search :null_double, <<-PATTERN
          (lvasgn $_
            (send
              $(send nil :instance_double
                ...) :as_null_object))
        PATTERN

        def_node_search :have_received_usage, <<-PATTERN
          (send
            (send nil :expect
            (lvar $_)) :to
            (send nil :have_received
            ...)
          ...)
        PATTERN

        def on_block(node)
          return unless example?(node)

          null_double(node) do |var, receiver|
            have_received_usage(node) do |expected|
              add_offense(receiver, :expression) if expected == var
            end
          end
        end

        def autocorrect(node)
          lambda do |corrector|
            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
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
rubocop-rspec-1.18.0 lib/rubocop/cop/rspec/instance_spy.rb
rubocop-rspec-1.17.1 lib/rubocop/cop/rspec/instance_spy.rb
rubocop-rspec-1.17.0 lib/rubocop/cop/rspec/instance_spy.rb
rubocop-rspec-1.16.0 lib/rubocop/cop/rspec/instance_spy.rb
rubocop-rspec-1.15.1 lib/rubocop/cop/rspec/instance_spy.rb
rubocop-rspec-1.15.0 lib/rubocop/cop/rspec/instance_spy.rb
rubocop-rspec-1.14.0 lib/rubocop/cop/rspec/instance_spy.rb
rubocop-rspec-1.13.0 lib/rubocop/cop/rspec/instance_spy.rb