Sha256: dbfc3754327f0e540e9d7fd28d27e07d46ee9a7ac5abc71dcc7df0a477b9fe91

Contents?: true

Size: 1.8 KB

Versions: 22

Compression:

Stored size: 1.8 KB

Contents

require 'mocha/mockery'
require 'mocha/class_method'
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
    def stubba_method
      Mocha::ClassMethod
    end

    # @private
    class AnyInstance

      def initialize(klass)
        @stubba_object = klass
      end

      def mocha
        @mocha ||= Mocha::Mockery.instance.mock_impersonating_any_instance_of(@stubba_object)
      end

      def stubba_method
        Mocha::AnyInstanceMethod
      end

      def stubba_object
        @stubba_object
      end

      def method_exists?(method, include_public_methods = true)
        if include_public_methods
          return true if @stubba_object.public_instance_methods(include_superclass_methods = true).include?(method)
        end
        return true if @stubba_object.protected_instance_methods(include_superclass_methods = true).include?(method)
        return true if @stubba_object.private_instance_methods(include_superclass_methods = true).include?(method)
        return false
      end

    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

  end

end

Version data entries

22 entries across 18 versions & 5 rubygems

Version Path
apl-library-0.0.90 vendor/bundle/ruby/1.9.1/gems/mocha-1.0.0/lib/mocha/class_methods.rb
apl-library-0.0.90 vendor/bundle/ruby/2.1.0/gems/mocha-1.0.0/lib/mocha/class_methods.rb
apl-library-0.0.90 vendor/bundle/ruby/2.1.0/gems/apl-library-0.0.90/vendor/bundle/ruby/2.1.0/gems/mocha-1.0.0/lib/mocha/class_methods.rb
apl-library-0.0.90 vendor/bundle/ruby/2.1.0/gems/apl-library-0.0.90/vendor/bundle/ruby/1.9.1/gems/mocha-1.0.0/lib/mocha/class_methods.rb
mocha-1.1.0 lib/mocha/class_methods.rb
mocha-1.0.0 lib/mocha/class_methods.rb
mocha-1.0.0.alpha lib/mocha/class_methods.rb
tnargav-1.3.3 vendor/bundle/ruby/1.9.1/gems/mocha-0.14.0/lib/mocha/class_methods.rb
challah-1.0.0 vendor/bundle/gems/mocha-0.14.0/lib/mocha/class_methods.rb
tnargav-1.2.3 vendor/bundle/ruby/1.9.1/gems/mocha-0.14.0/lib/mocha/class_methods.rb
mocha-0.14.0 lib/mocha/class_methods.rb
challah-1.0.0.beta3 vendor/bundle/gems/mocha-0.13.3/lib/mocha/class_methods.rb
mocha-0.14.0.alpha lib/mocha/class_methods.rb
challah-1.0.0.beta2 vendor/bundle/gems/mocha-0.13.3/lib/mocha/class_methods.rb
challah-1.0.0.beta vendor/bundle/gems/mocha-0.13.3/lib/mocha/class_methods.rb
mocha-0.13.3 lib/mocha/class_methods.rb
mocha-0.13.2 lib/mocha/class_methods.rb
mocha-0.13.1 lib/mocha/class_methods.rb
challah-rolls-0.2.0 vendor/bundle/gems/mocha-0.13.0/lib/mocha/class_methods.rb
challah-rolls-0.2.0 vendor/bundle/gems/challah-0.8.3/vendor/bundle/gems/mocha-0.13.0/lib/mocha/class_methods.rb