Sha256: 9be52b0c55077ee29c43b845bba1ee5e1356f51583e057eaa2b2bdf860647858

Contents?: true

Size: 1.1 KB

Versions: 3

Compression:

Stored size: 1.1 KB

Contents

module RuboCop
  module Cop
    module RSpec
      # Check that instances are not being stubbed globally.
      #
      # Prefer 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))
        end
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
rubocop-rspec-1.12.0 lib/rubocop/cop/rspec/any_instance.rb
rubocop-rspec-1.11.0 lib/rubocop/cop/rspec/any_instance.rb
rubocop-rspec-1.10.0 lib/rubocop/cop/rspec/any_instance.rb