Class: RSpec::Mocks::Proxy

### Home Classes Methods

### In Files

- lib/rspec/mocks/proxy.rb

### Parent

Object

### Methods

- ::new
- ::prepended_modules_of
- #add_message_expectation
- #add_simple_expectation
- #add_simple_stub
- #add_stub
- #as_null_object
- #build_expectation
- #check_for_unexpected_arguments
- #ensure_implemented
- #has_negative_expectation?
- #message_received
- #messages_arg_list
- #null_object?
- #original_method_handle_for
- #prepended_modules_of_singleton_class
- #raise_missing_default_stub_error
- #raise_unexpected_message_error
- #received_message?
- #record_message_received
- #remove_stub
- #remove_stub_if_present
- #replay_received_message_on
- #reset
- #verify
- #visibility_for

### 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::Proxy

@private

### Constants

DEFAULT_MESSAGE_EXPECTATION_OPTS
SpecificMessage

### Attributes

object[R]

@private

### Public Class Methods

new(object, order_group, options={}) click to toggle source

@private

    # File lib/rspec/mocks/proxy.rb, line 17
    def initialize(object, order_group, options={})
      @object = object
      @order_group = order_group
      @error_generator = ErrorGenerator.new(object)
      @messages_received = []
      @options = options
      @null_object = false
      @method_doubles = Hash.new { |h, k| h[k] = MethodDouble.new(@object, k, self) }
    end

prepended_modules_of(klass) click to toggle source

    # File lib/rspec/mocks/proxy.rb, line 216
    def self.prepended_modules_of(klass)
      ancestors = klass.ancestors

      # `|| 0` is necessary for Ruby 2.0, where the singleton class
      # is only in the ancestor list when there are prepended modules.
      singleton_index = ancestors.index(klass) || 0

      ancestors[0, singleton_index]
    end

### Public Instance Methods

add_message_expectation(method_name, opts=DEFAULT_MESSAGE_EXPECTATION_OPTS, &block) click to toggle source

@private

    # File lib/rspec/mocks/proxy.rb, line 51
    def add_message_expectation(method_name, opts=DEFAULT_MESSAGE_EXPECTATION_OPTS, &block)
      location = opts.fetch(:expected_from) { CallerFilter.first_non_rspec_line }
      meth_double = method_double_for(method_name)

      if null_object? && !block
        meth_double.add_default_stub(@error_generator, @order_group, location, opts) do
          @object
        end
      end

      meth_double.add_expectation @error_generator, @order_group, location, opts, &block
    end

add_simple_expectation(method_name, response, location) click to toggle source

@private

    # File lib/rspec/mocks/proxy.rb, line 65
    def add_simple_expectation(method_name, response, location)
      method_double_for(method_name).add_simple_expectation method_name, response, @error_generator, location
    end

add_simple_stub(method_name, response) click to toggle source

@private

    # File lib/rspec/mocks/proxy.rb, line 122
    def add_simple_stub(method_name, response)
      method_double_for(method_name).add_simple_stub method_name, response
    end

add_stub(method_name, opts={}, &implementation) click to toggle source

@private

    # File lib/rspec/mocks/proxy.rb, line 116
    def add_stub(method_name, opts={}, &implementation)
      location = opts.fetch(:expected_from) { CallerFilter.first_non_rspec_line }
      method_double_for(method_name).add_stub @error_generator, @order_group, location, opts, &implementation
    end

as_null_object() click to toggle source

@private Tells the object to ignore any messages that aren't explicitly set as stubs or message expectations.

    # File lib/rspec/mocks/proxy.rb, line 38
    def as_null_object
      @null_object = true
      @object
    end

build_expectation(method_name) click to toggle source

@private

    # File lib/rspec/mocks/proxy.rb, line 70
    def build_expectation(method_name)
      meth_double = method_double_for(method_name)

      meth_double.build_expectation(
        @error_generator,
        @order_group
      )
    end

check_for_unexpected_arguments(expectation) click to toggle source

@private

    # File lib/rspec/mocks/proxy.rb, line 101
    def check_for_unexpected_arguments(expectation)
      return if @messages_received.empty?

      return if @messages_received.any? { |method_name, args, _| expectation.matches?(method_name, *args) }

      name_but_not_args, others = @messages_received.partition do |(method_name, args, _)|
        expectation.matches_name_but_not_args(method_name, *args)
      end

      return if name_but_not_args.empty? && !others.empty?

      expectation.raise_unexpected_message_args_error(name_but_not_args.map { |args| args[1] })
    end

ensure_implemented(*_args) click to toggle source

@private

    # File lib/rspec/mocks/proxy.rb, line 12
    def ensure_implemented(*_args)
      # noop for basic proxies, see VerifyingProxy for behaviour.
    end

has_negative_expectation?(message) click to toggle source

@private

    # File lib/rspec/mocks/proxy.rb, line 157
    def has_negative_expectation?(message)
      method_double_for(message).expectations.find { |expectation| expectation.negative_expectation_for?(message) }
    end

message_received(message, *args, &block) click to toggle source

@private

    # File lib/rspec/mocks/proxy.rb, line 168
    def message_received(message, *args, &block)
      record_message_received message, *args, &block

      expectation = find_matching_expectation(message, *args)
      stub = find_matching_method_stub(message, *args)

      if (stub && expectation && expectation.called_max_times?) || (stub && !expectation)
        expectation.increase_actual_received_count! if expectation && expectation.actual_received_count_matters?
        if (expectation = find_almost_matching_expectation(message, *args))
          expectation.advise(*args) unless expectation.expected_messages_received?
        end
        stub.invoke(nil, *args, &block)
      elsif expectation
        expectation.unadvise(messages_arg_list)
        expectation.invoke(stub, *args, &block)
      elsif (expectation = find_almost_matching_expectation(message, *args))
        expectation.advise(*args) if null_object? unless expectation.expected_messages_received?

        if null_object? || !has_negative_expectation?(message)
          expectation.raise_unexpected_message_args_error([args])
        end
      elsif (stub = find_almost_matching_stub(message, *args))
        stub.advise(*args)
        raise_missing_default_stub_error(stub, [args])
      elsif Class === @object
        @object.superclass.__send__(message, *args, &block)
      else
        @object.__send__(:method_missing, message, *args, &block)
      end
    end

messages_arg_list() click to toggle source

@private

    # File lib/rspec/mocks/proxy.rb, line 152
    def messages_arg_list
      @messages_received.map { |_, args, _| args }
    end

null_object?() click to toggle source

@private

    # File lib/rspec/mocks/proxy.rb, line 31
    def null_object?
      @null_object
    end

original_method_handle_for(_message) click to toggle source

@private

    # File lib/rspec/mocks/proxy.rb, line 44
    def original_method_handle_for(_message)
      nil
    end

prepended_modules_of_singleton_class() click to toggle source

    # File lib/rspec/mocks/proxy.rb, line 226
    def prepended_modules_of_singleton_class
      @prepended_modules_of_singleton_class ||= RSpec::Mocks::Proxy.prepended_modules_of(@object.singleton_class)
    end

raise_missing_default_stub_error(expectation, args_for_multiple_calls) click to toggle source

@private

    # File lib/rspec/mocks/proxy.rb, line 205
    def raise_missing_default_stub_error(expectation, args_for_multiple_calls)
      @error_generator.raise_missing_default_stub_error(expectation, args_for_multiple_calls)
    end

raise_unexpected_message_error(method_name, args) click to toggle source

@private

    # File lib/rspec/mocks/proxy.rb, line 200
    def raise_unexpected_message_error(method_name, args)
      @error_generator.raise_unexpected_message_error method_name, args
    end

received_message?(method_name, *args, &block) click to toggle source

@private

    # File lib/rspec/mocks/proxy.rb, line 147
    def received_message?(method_name, *args, &block)
      @messages_received.any? { |array| array == [method_name, args, block] }
    end

record_message_received(message, *args, &block) click to toggle source

@private

    # File lib/rspec/mocks/proxy.rb, line 162
    def record_message_received(message, *args, &block)
      @order_group.invoked SpecificMessage.new(object, message, args)
      @messages_received << [message, args, block]
    end

remove_stub(method_name) click to toggle source

@private

    # File lib/rspec/mocks/proxy.rb, line 127
    def remove_stub(method_name)
      method_double_for(method_name).remove_stub
    end

remove_stub_if_present(method_name) click to toggle source

@private

    # File lib/rspec/mocks/proxy.rb, line 132
    def remove_stub_if_present(method_name)
      method_double_for(method_name).remove_stub_if_present
    end

replay_received_message_on(expectation, &block) click to toggle source

@private

    # File lib/rspec/mocks/proxy.rb, line 80
    def replay_received_message_on(expectation, &block)
      expected_method_name = expectation.message
      meth_double = method_double_for(expected_method_name)

      if meth_double.expectations.any?
        @error_generator.raise_expectation_on_mocked_method(expected_method_name)
      end

      unless null_object? || meth_double.stubs.any?
        @error_generator.raise_expectation_on_unstubbed_method(expected_method_name)
      end

      @messages_received.each do |(actual_method_name, args, _)|
        next unless expectation.matches?(actual_method_name, *args)

        expectation.safe_invoke(nil)
        block.call(*args) if block
      end
    end

reset() click to toggle source

@private

    # File lib/rspec/mocks/proxy.rb, line 142
    def reset
      @messages_received.clear
    end

verify() click to toggle source

@private

    # File lib/rspec/mocks/proxy.rb, line 137
    def verify
      @method_doubles.each_value { |d| d.verify }
    end

visibility_for(_method_name) click to toggle source

@private

    # File lib/rspec/mocks/proxy.rb, line 210
    def visibility_for(_method_name)
      # This is the default (for test doubles). Subclasses override this.
      :public
    end

[Validate]

Generated with the Darkfish Rdoc Generator 2.