Sha256: 00af33941b4511582949a5d0604dc8bf368bf061346e0f942aec1892a78d2576

Contents?: true

Size: 1.03 KB

Versions: 3

Compression:

Stored size: 1.03 KB

Contents

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, MESSAGE % { method: method_name })
        end
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
rubocop-rspec-1.6.0 lib/rubocop/cop/rspec/any_instance.rb
rubocop-rspec-1.5.3 lib/rubocop/cop/rspec/any_instance.rb
rubocop-rspec-1.5.2 lib/rubocop/cop/rspec/any_instance.rb