Module: RSpec::Mocks::PartialClassDoubleProxyMethods

### Home Classes Methods

### In Files

- lib/rspec/mocks/proxy.rb

### Methods

- ::new
- #original_method_handle_for
- #original_unbound_method_handle_from_ancestor_for
- #superclass_proxy

### Class/Module Index

Quicksearch

- RSpec
- RSpec::Mocks
- RSpec::Mocks::AllowanceTarget
- RSpec::Mocks::AndReturnImplementation
- RSpec::Mocks::AndWrapOriginalImplementation
- RSpec::Mocks::AndYieldImplementation
- RSpec::Mocks::AnyInstance
- RSpec::Mocks::AnyInstance::Chain
- RSpec::Mocks::AnyInstance::Chain::Customizations
- RSpec::Mocks::AnyInstance::ErrorGenerator
- RSpec::Mocks::AnyInstance::ExpectChainChain
- RSpec::Mocks::AnyInstance::ExpectationChain
- RSpec::Mocks::AnyInstance::FluentInterfaceProxy
- RSpec::Mocks::AnyInstance::MessageChains
- RSpec::Mocks::AnyInstance::PositiveExpectationChain
- RSpec::Mocks::AnyInstance::Proxy
- RSpec::Mocks::AnyInstance::Recorder
- RSpec::Mocks::AnyInstance::StubChain
- RSpec::Mocks::AnyInstance::StubChainChain
- RSpec::Mocks::AnyInstanceAllowanceTarget
- RSpec::Mocks::AnyInstanceExpectationTarget
- RSpec::Mocks::ArgumentListMatcher
- RSpec::Mocks::ArgumentMatchers
- RSpec::Mocks::ArgumentMatchers::AnyArgMatcher
- RSpec::Mocks::ArgumentMatchers::AnyArgsMatcher
- RSpec::Mocks::ArgumentMatchers::ArrayIncludingMatcher
- RSpec::Mocks::ArgumentMatchers::BaseHashMatcher
- RSpec::Mocks::ArgumentMatchers::BooleanMatcher
- RSpec::Mocks::ArgumentMatchers::DuckTypeMatcher
- RSpec::Mocks::ArgumentMatchers::HashExcludingMatcher
- RSpec::Mocks::ArgumentMatchers::HashIncludingMatcher
- RSpec::Mocks::ArgumentMatchers::InstanceOf
- RSpec::Mocks::ArgumentMatchers::KindOf
- RSpec::Mocks::ArgumentMatchers::NoArgsMatcher
- RSpec::Mocks::ArgumentMatchers::SingletonMatcher
- RSpec::Mocks::CallbackInvocationStrategy
- RSpec::Mocks::ClassNewMethodReference
- RSpec::Mocks::ClassVerifyingDouble
- RSpec::Mocks::Configuration
- RSpec::Mocks::Constant
- RSpec::Mocks::ConstantMutator
- RSpec::Mocks::ConstantMutator::BaseMutator
- RSpec::Mocks::ConstantMutator::ConstantHider
- RSpec::Mocks::ConstantMutator::DefinedConstantReplacer
- RSpec::Mocks::ConstantMutator::UndefinedConstantSetter
- RSpec::Mocks::DirectObjectReference
- RSpec::Mocks::Double
- RSpec::Mocks::ErrorGenerator
- RSpec::Mocks::ExampleMethods
- RSpec::Mocks::ExampleMethods::ExpectHost
- RSpec::Mocks::ExpectChain
- RSpec::Mocks::ExpectationTarget
- RSpec::Mocks::Implementation
- RSpec::Mocks::InstanceMethodReference
- RSpec::Mocks::InstanceMethodStasher
- RSpec::Mocks::InstanceVerifyingDouble
- RSpec::Mocks::MarshalExtension
- RSpec::Mocks::Matchers
- RSpec::Mocks::Matchers::ExpectationCustomization
- RSpec::Mocks::Matchers::HaveReceived
- RSpec::Mocks::Matchers::Receive
- RSpec::Mocks::Matchers::Receive::DefaultDescribable
- RSpec::Mocks::Matchers::ReceiveMessageChain
- RSpec::Mocks::Matchers::ReceiveMessages
- RSpec::Mocks::MessageChain
- RSpec::Mocks::MessageExpectation
- RSpec::Mocks::MessageExpectation::ImplementationDetails
- RSpec::Mocks::MethodDouble
- RSpec::Mocks::MethodReference
- RSpec::Mocks::Mutex
- RSpec::Mocks::NamedObjectReference
- RSpec::Mocks::NestedSpace
- RSpec::Mocks::NoCallbackInvocationStrategy
- RSpec::Mocks::ObjectMethodReference
- RSpec::Mocks::ObjectReference
- RSpec::Mocks::ObjectVerifyingDouble
- RSpec::Mocks::ObjectVerifyingDoubleMethods
- RSpec::Mocks::OrderGroup
- RSpec::Mocks::PartialClassDoubleProxy
- RSpec::Mocks::PartialClassDoubleProxyMethods
- RSpec::Mocks::PartialDoubleProxy
- RSpec::Mocks::Proxy
- RSpec::Mocks::ProxyForNil
- RSpec::Mocks::ReentrantMutex
- RSpec::Mocks::RootSpace
- RSpec::Mocks::SimpleMessageExpectation
- RSpec::Mocks::Space
- RSpec::Mocks::StubChain
- RSpec::Mocks::Syntax
- RSpec::Mocks::TargetBase
- RSpec::Mocks::TestDouble
- RSpec::Mocks::TestDoubleFormatter
- RSpec::Mocks::TestDoubleProxy
- RSpec::Mocks::VerifyingDouble
- RSpec::Mocks::VerifyingDouble::SilentIO
- RSpec::Mocks::VerifyingExistingClassNewMethodDouble
- RSpec::Mocks::VerifyingExistingMethodDouble
- RSpec::Mocks::VerifyingMessageExpectation
- RSpec::Mocks::VerifyingMethodDouble
- RSpec::Mocks::VerifyingPartialClassDoubleProxy
- RSpec::Mocks::VerifyingPartialDoubleProxy
- RSpec::Mocks::VerifyingProxy
- RSpec::Mocks::VerifyingProxyMethods
- RSpec::Mocks::Version
- BasicObject
- Class

No matching classes.

# RSpec::Mocks::PartialClassDoubleProxyMethods

@private When we mock or stub a method on a class, we have to treat it a bit different, because normally singleton method definitions only affect the object on which they are defined, but on classes they affect subclasses, too. As a result, we need some special handling to get the original method.

### Public Class Methods

new(source_space, *args) click to toggle source

    # File lib/rspec/mocks/proxy.rb, line 343
    def initialize(source_space, *args)
      @source_space = source_space
      super(*args)
    end

### Public Instance Methods

original_method_handle_for(message) click to toggle source

Consider this situation:

    class A; end
    class B < A; end

    allow(A).to receive(:new)
    expect(B).to receive(:new).and_call_original

When getting the original definition for `B.new`, we cannot rely purely on using `B.method(:new)` before our redefinition is defined on `B`, because `B.method(:new)` will return a method that will execute the stubbed version of the method on `A` since singleton methods on classes are in the lookup hierarchy.

To do it properly, we need to find the original definition of `new` from `A` from before `A` was stubbed, and we need to rebind it to `B` so that it will run with the proper `self`.

That's what this method (together with `original_unbound_method_handle_from_ancestor_for`) does.

    # File lib/rspec/mocks/proxy.rb, line 368
    def original_method_handle_for(message)
      unbound_method = superclass_proxy &&
        superclass_proxy.original_unbound_method_handle_from_ancestor_for(message.to_sym)

      return super unless unbound_method
      unbound_method.bind(object)
    end

### Protected Instance Methods

original_unbound_method_handle_from_ancestor_for(message) click to toggle source

    # File lib/rspec/mocks/proxy.rb, line 378
    def original_unbound_method_handle_from_ancestor_for(message)
      method_double = @method_doubles.fetch(message) do
        # The fact that there is no method double for this message indicates
        # that it has not been redefined by rspec-mocks. We need to continue
        # looking up the ancestor chain.
        return superclass_proxy &&
          superclass_proxy.original_unbound_method_handle_from_ancestor_for(message)
      end

      method_double.original_method.unbind
    end

superclass_proxy() click to toggle source

    # File lib/rspec/mocks/proxy.rb, line 390
    def superclass_proxy
      return @superclass_proxy if defined?(@superclass_proxy)

      if (superclass = object.superclass)
        @superclass_proxy = @source_space.superclass_proxy_for(superclass)
      else
        @superclass_proxy = nil
      end
    end

[Validate]

Generated with the Darkfish Rdoc Generator 2.