Sha256: 68c1552e42580ebecae043dbdc3f8c1be557153905a19b80aea215cf19d7490b

Contents?: true

Size: 1.13 KB

Versions: 20

Compression:

Stored size: 1.13 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,
              location: :expression,
              message: format(MSG, method: method)
            )
          end
        end
      end
    end
  end
end

Version data entries

20 entries across 20 versions & 1 rubygems

Version Path
rubocop-rspec-1.32.0 lib/rubocop/cop/rspec/any_instance.rb
rubocop-rspec-1.31.0 lib/rubocop/cop/rspec/any_instance.rb
rubocop-rspec-1.30.1 lib/rubocop/cop/rspec/any_instance.rb
rubocop-rspec-1.30.0 lib/rubocop/cop/rspec/any_instance.rb
rubocop-rspec-1.29.1 lib/rubocop/cop/rspec/any_instance.rb
rubocop-rspec-1.29.0 lib/rubocop/cop/rspec/any_instance.rb
rubocop-rspec-1.28.0 lib/rubocop/cop/rspec/any_instance.rb
rubocop-rspec-1.27.0 lib/rubocop/cop/rspec/any_instance.rb
rubocop-rspec-1.26.0 lib/rubocop/cop/rspec/any_instance.rb
rubocop-rspec-1.25.1 lib/rubocop/cop/rspec/any_instance.rb
rubocop-rspec-1.25.0 lib/rubocop/cop/rspec/any_instance.rb
rubocop-rspec-1.24.0 lib/rubocop/cop/rspec/any_instance.rb
rubocop-rspec-1.23.0 lib/rubocop/cop/rspec/any_instance.rb
rubocop-rspec-1.22.2 lib/rubocop/cop/rspec/any_instance.rb
rubocop-rspec-1.22.1 lib/rubocop/cop/rspec/any_instance.rb
rubocop-rspec-1.22.0 lib/rubocop/cop/rspec/any_instance.rb
rubocop-rspec-1.21.0 lib/rubocop/cop/rspec/any_instance.rb
rubocop-rspec-1.20.1 lib/rubocop/cop/rspec/any_instance.rb
rubocop-rspec-1.20.0 lib/rubocop/cop/rspec/any_instance.rb
rubocop-rspec-1.19.0 lib/rubocop/cop/rspec/any_instance.rb