Sha256: d7c9456b78e18fda40fe598e6bc9f79d63e6f4477b6e2ff32cafdff38cd6a0fe

Contents?: true

Size: 1.05 KB

Versions: 4

Compression:

Stored size: 1.05 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
        MSG = 'Avoid stubbing using `%<method>s`.'.freeze

        def_node_matcher :disallowed_stub, <<-PATTERN
          (send _ ${:any_instance :allow_any_instance_of :expect_any_instance_of} ...)
        PATTERN

        def on_send(node)
          disallowed_stub(node) do |method|
            add_offense(node, :expression, format(MSG, method: method))
          end
        end
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
rubocop-rspec-1.18.0 lib/rubocop/cop/rspec/any_instance.rb
rubocop-rspec-1.17.1 lib/rubocop/cop/rspec/any_instance.rb
rubocop-rspec-1.17.0 lib/rubocop/cop/rspec/any_instance.rb
rubocop-rspec-1.16.0 lib/rubocop/cop/rspec/any_instance.rb