Sha256: 5a0aa20b7f76c75c8886f56694bab8b464eeb541d06b3439fe627e11eef51d81

Contents?: true

Size: 1.17 KB

Versions: 2

Compression:

Stored size: 1.17 KB

Contents

# encoding: utf-8

module RuboCop
  module Cop
    module RSpec
      # Pefer instance doubles over stubbing any instance of a class
      #
      # @example
      #   # bad
      #   describe MyClass do
      #     before { allow_any_instance_of(MyClass).to receive(:foo) }
      #   end
      #
      #   # good
      #   describe MyClass do
      #     let(:my_instance) { instance_double(MyClass) }
      #
      #     before do
      #       allow(MyClass).to receive(:new).and_return(my_instance)
      #       allow(my_instance).to receive(:foo)
      #     end
      #   end
      class AnyInstance < Cop
        MESSAGE = 'Avoid stubbing using `%{method}`'.freeze

        ANY_INSTANCE_METHODS = [
          :any_instance,
          :allow_any_instance_of,
          :expect_any_instance_of
        ].freeze

        def on_send(node)
          _receiver, method_name, *_args = *node
          return unless ANY_INSTANCE_METHODS.include?(method_name)

          add_offense(
            node,
            :expression,
            format(
              MESSAGE % { method: method_name },
              node.loc.expression.source
            )
          )
        end
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
rubocop-rspec-1.5.1 lib/rubocop/cop/rspec/any_instance.rb
rubocop-rspec-1.5.0 lib/rubocop/cop/rspec/any_instance.rb