Sha256: 431fced3c9b02b8d10cbc3992834c521e5799d0b79da77a273b9325ab30480de

Contents?: true

Size: 1.11 KB

Versions: 12

Compression:

Stored size: 1.11 KB

Contents

# frozen_string_literal: true

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`.'

        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,
              message: format(MSG, method: method)
            )
          end
        end
      end
    end
  end
end

Version data entries

12 entries across 12 versions & 1 rubygems

Version Path
rubocop-rspec-1.42.0 lib/rubocop/cop/rspec/any_instance.rb
rubocop-rspec-1.41.0 lib/rubocop/cop/rspec/any_instance.rb
rubocop-rspec-1.40.0 lib/rubocop/cop/rspec/any_instance.rb
rubocop-rspec-1.39.0 lib/rubocop/cop/rspec/any_instance.rb
rubocop-rspec-1.38.1 lib/rubocop/cop/rspec/any_instance.rb
rubocop-rspec-1.38.0 lib/rubocop/cop/rspec/any_instance.rb
rubocop-rspec-1.37.1 lib/rubocop/cop/rspec/any_instance.rb
rubocop-rspec-1.37.0 lib/rubocop/cop/rspec/any_instance.rb
rubocop-rspec-1.36.0 lib/rubocop/cop/rspec/any_instance.rb
rubocop-rspec-1.35.0 lib/rubocop/cop/rspec/any_instance.rb
rubocop-rspec-1.34.1 lib/rubocop/cop/rspec/any_instance.rb
rubocop-rspec-1.34.0 lib/rubocop/cop/rspec/any_instance.rb