Sha256: b35c43e400c673e252f619a0b52b9a4e0b01a6889d9c257f9318aa32060a9293

Contents?: true

Size: 1.93 KB

Versions: 21

Compression:

Stored size: 1.93 KB

Contents

require 'mocha/mockery'
require 'mocha/any_instance_method'

module Mocha
  # Methods added to all classes to allow mocking and stubbing on real (i.e. non-mock) objects.
  module ClassMethods
    # @private
    class AnyInstance
      def initialize(klass)
        @stubba_object = klass
      end

      def mocha(instantiate = true)
        if instantiate
          @mocha ||= Mocha::Mockery.instance.mock_impersonating_any_instance_of(@stubba_object)
        else
          defined?(@mocha) ? @mocha : nil
        end
      end

      def stubba_method
        Mocha::AnyInstanceMethod
      end

      def stubba_class
        @stubba_object
      end

      def respond_to?(symbol, include_all = false)
        @stubba_object.allocate.respond_to?(symbol.to_sym, include_all)
      end

      attr_reader :stubba_object
    end

    # @return [Mock] a mock object which will detect calls to any instance of this class.
    # @raise [StubbingError] if attempting to stub method which is not allowed.
    #
    # @example Return false to invocation of +Product#save+ for any instance of +Product+.
    #   Product.any_instance.stubs(:save).returns(false)
    #   product_1 = Product.new
    #   assert_equal false, product_1.save
    #   product_2 = Product.new
    #   assert_equal false, product_2.save
    def any_instance
      if frozen?
        raise StubbingError.new("can't stub method on frozen object: #{mocha_inspect}.any_instance", caller)
      end
      @any_instance ||= AnyInstance.new(self)
    end

    # @private
    # rubocop:disable Metrics/CyclomaticComplexity
    def __method_visibility__(method, include_public_methods = true)
      (include_public_methods && public_method_defined?(method) && :public) ||
        (protected_method_defined?(method) && :protected) ||
        (private_method_defined?(method) && :private)
    end
    # rubocop:enable Metrics/CyclomaticComplexity
    alias_method :__method_exists__?, :__method_visibility__
  end
end

Version data entries

21 entries across 21 versions & 2 rubygems

Version Path
mocha-2.5.0 lib/mocha/class_methods.rb
mocha-2.4.5 lib/mocha/class_methods.rb
mocha-2.4.4 lib/mocha/class_methods.rb
mocha-2.4.3 lib/mocha/class_methods.rb
mocha-2.4.2 lib/mocha/class_methods.rb
mocha-2.4.1 lib/mocha/class_methods.rb
mocha-2.4.0 lib/mocha/class_methods.rb
mocha-2.2.0 lib/mocha/class_methods.rb
mocha-2.1.0 lib/mocha/class_methods.rb
mocha-2.0.4 lib/mocha/class_methods.rb
mocha-2.0.3 lib/mocha/class_methods.rb
fluent-plugin-google-cloud-logging-on-prem-0.1.0 vendor/ruby/3.1.0/gems/mocha-2.0.2/lib/mocha/class_methods.rb
mocha-2.0.2 lib/mocha/class_methods.rb
mocha-1.16.1 lib/mocha/class_methods.rb
mocha-1.15.1 lib/mocha/class_methods.rb
mocha-2.0.1 lib/mocha/class_methods.rb
mocha-2.0.0 lib/mocha/class_methods.rb
mocha-1.16.0 lib/mocha/class_methods.rb
mocha-2.0.0.alpha.1 lib/mocha/class_methods.rb
mocha-2.0.0.alpha lib/mocha/class_methods.rb