Class: FlexMock::PartialMockProxy

### Home Classes Methods

### In Files

- lib/flexmock/deprecated_methods.rb
- lib/flexmock/partial_mock.rb

### Parent

Object

### Methods

- ::new
- #add_mock_method
- #any_instance
- #flexmock_based_on
- #flexmock_calls
- #flexmock_container
- #flexmock_container=
- #flexmock_define_expectation
- #flexmock_expectations_for
- #flexmock_get
- #flexmock_invoke_original
- #flexmock_received?
- #flexmock_teardown
- #flexmock_verify
- #new_instances
- #should_receive

### Included Modules

- FlexMock::Ordering

### Files

- CHANGES
- GoogleExample.rdoc
- rspec_examples_spec.rdoc
- test_unit_examples_test.rdoc
- index.rdoc
- flexmock-0.4.0.rdoc
- flexmock-0.4.1.rdoc
- flexmock-0.4.2.rdoc
- flexmock-0.4.3.rdoc
- flexmock-0.5.0.rdoc
- flexmock-0.5.1.rdoc
- flexmock-0.6.0.rdoc
- flexmock-0.6.1.rdoc
- flexmock-0.6.2.rdoc
- flexmock-0.6.3.rdoc
- flexmock-0.6.4.rdoc
- flexmock-0.7.0.rdoc
- flexmock-0.7.1.rdoc
- flexmock-0.8.0.rdoc
- flexmock-0.8.2.rdoc
- flexmock-0.8.3.rdoc
- flexmock-0.8.4.rdoc
- flexmock-0.8.5.rdoc
- flexmock-0.9.0.rdoc
- flexmock-1.0.0.rdoc
- flexmock-1.0.3.rdoc
- flexmock-1.0.4.rdoc
- flexmock-1.1.0.rdoc
- flexmock-1.2.0.rdoc
- flexmock-1.3.0.rdoc
- flexmock-1.3.1.rdoc

### Class/Module Index

Quicksearch

- FlexMock
- FlexMock::AnyMatcher
- FlexMock::ArgumentMatching
- FlexMock::ArgumentTypes
- FlexMock::AtLeastCountValidator
- FlexMock::AtMostCountValidator
- FlexMock::CompositeExpectation
- FlexMock::CountValidator
- FlexMock::DefaultFrameworkAdapter
- FlexMock::DefaultFrameworkAdapter::AssertionFailedError
- FlexMock::DuckMatcher
- FlexMock::EqualMatcher
- FlexMock::ExactCountValidator
- FlexMock::Expectation
- FlexMock::ExpectationDirector
- FlexMock::ExpectationRecorder
- FlexMock::ExplicitNeeded
- FlexMock::GenericTestCase
- FlexMock::HashMatcher
- FlexMock::MockContainer
- FlexMock::MockContainerHelper
- FlexMock::MockError
- FlexMock::OptionalProcMatcher
- FlexMock::Ordering
- FlexMock::PartialMockProxy
- FlexMock::ProcMatcher
- FlexMock::RSpecFrameworkAdapter
- FlexMock::RSpecFrameworkAdapter::AssertionFailedError
- FlexMock::RSpecMatchers
- FlexMock::RSpecMatchers::HaveReceived
- FlexMock::Recorder
- FlexMock::SpyDescribers
- FlexMock::TestCase
- FlexMock::TestUnitAssertions
- FlexMock::TestUnitFrameworkAdapter
- FlexMock::Undefined
- FlexMock::UsageError
- FlexMock::UseContainer
- FlexMock::Version
- MiniTest
- MiniTest::Unit
- MiniTest::Unit::TestCase
- Test
- Test::Unit
- Test::Unit::TestCase
- Class
- Module
- Object
- Symbol

No matching classes.

# FlexMock::PartialMockProxy

######################################################################### PartialMockProxy is used to mate the mock framework to an existing object. The object is "enhanced" with a reference to a mock object (stored in `@flexmock_proxy`). When the `should_receive` method is sent to the proxy, it overrides the existing object's method by creating singleton method that forwards to the mock. When testing is complete, PartialMockProxy will erase the mocking infrastructure from the object being mocked (e.g. remove instance variables and mock singleton methods).

### Constants

MOCK_METHODS

  The following methods are added to partial mocks so that they can act like a mock.

### Attributes

mock[R]

### Public Class Methods

new(obj, mock, safe_mode) click to toggle source

Initialize a PartialMockProxy object.

    # File lib/flexmock/partial_mock.rb, line 42
    def initialize(obj, mock, safe_mode)
      @obj = obj
      @mock = mock
      @method_definitions = {}
      @methods_proxied = []
      unless safe_mode
        add_mock_method(:should_receive)
        MOCK_METHODS.each do |sym|
          unless @obj.respond_to?(sym)
            add_mock_method(sym)
          end
        end
      end
    end

### Public Instance Methods

add_mock_method(method_name) click to toggle source

    # File lib/flexmock/partial_mock.rb, line 98
    def add_mock_method(method_name)
      stow_existing_definition(method_name)
      sclass.module_eval do
        define_method(method_name) { |*args, &block|
          proxy = instance_variable_get("@flexmock_proxy")
          if proxy.nil?
            fail "Missing FlexMock proxy " +
              "(for method_name=#{method_name.inspect}, self=\#{self})"
          end
          proxy.send(method_name, *args, &block)
        }
      end
    end

any_instance(&block) click to toggle source

any_instance is present for backwards compatibility with version 0.5.0. @deprecated

    # File lib/flexmock/deprecated_methods.rb, line 53
    def any_instance(&block)
      $stderr.puts "any_instance is deprecated, use new_instances instead."
      new_instances(&block)
    end

flexmock_based_on(*args) click to toggle source

Forward the based on request.

    # File lib/flexmock/partial_mock.rb, line 209
    def flexmock_based_on(*args)
      @mock.flexmock_based_on(*args)
    end

flexmock_calls() click to toggle source

Forward to the mock

    # File lib/flexmock/partial_mock.rb, line 194
    def flexmock_calls
      @mock.flexmock_calls
    end

flexmock_container() click to toggle source

Forward to the mock's container.

    # File lib/flexmock/partial_mock.rb, line 184
    def flexmock_container
      @mock.flexmock_container
    end

flexmock_container=(container) click to toggle source

Set the proxy's mock container. This set value is ignored because the proxy always uses the container of its mock.

    # File lib/flexmock/partial_mock.rb, line 200
    def flexmock_container=(container)
    end

flexmock_define_expectation(location, *args) click to toggle source

    # File lib/flexmock/partial_mock.rb, line 87
    def flexmock_define_expectation(location, *args)
      ContainerHelper.parse_should_args(@mock, args) do |sym|
        unless @methods_proxied.include?(sym)
          hide_existing_method(sym)
        end
        ex = @mock.flexmock_define_expectation(location, sym)
        ex.mock = self
        ex
      end
    end

flexmock_expectations_for(method_name) click to toggle source

Forward the request for the expectation director to the mock.

    # File lib/flexmock/partial_mock.rb, line 204
    def flexmock_expectations_for(method_name)
      @mock.flexmock_expectations_for(method_name)
    end

flexmock_get() click to toggle source

Get the mock object for the partial mock.

    # File lib/flexmock/partial_mock.rb, line 58
    def flexmock_get
      @mock
    end

flexmock_invoke_original(method, args) click to toggle source

    # File lib/flexmock/partial_mock.rb, line 161
    def flexmock_invoke_original(method, args)
      invoke_original(method, args)
    end

flexmock_received?(*args) click to toggle source

Forward to the mock

    # File lib/flexmock/partial_mock.rb, line 189
    def flexmock_received?(*args)
      @mock.flexmock_received?(*args)
    end

flexmock_teardown() click to toggle source

Remove all traces of the mocking framework from the existing object.

    # File lib/flexmock/partial_mock.rb, line 172
    def flexmock_teardown
      if ! detached?
        @methods_proxied.each do |method_name|
          remove_current_method(method_name)
          restore_original_definition(method_name)
        end
        @obj.instance_variable_set("@flexmock_proxy", nil)
        @obj = nil
      end
    end

flexmock_verify() click to toggle source

Verify that the mock has been properly called. After verification, detach the mocking infrastructure from the existing object.

    # File lib/flexmock/partial_mock.rb, line 167
    def flexmock_verify
      @mock.flexmock_verify
    end

should_receive(...) click to toggle source
new_instances { |instance| instance.should_receive(...) }

new_instances is a short cut method for overriding the behavior of any new instances created via a mocked class object.

By default, new_instances will mock the behaviour of the :new method. If you wish to mock a different set of class methods, just pass a list of symbols to as arguments. (previous versions also mocked :allocate by default. If you need :allocate to be mocked, just request it explicitly).

For example, to stub only objects created by :make (and not :new), use:

    flexmock(ClassName).new_instances(:make).should_receive(...)

    # File lib/flexmock/partial_mock.rb, line 130
    def new_instances(*allocators, &block)
      fail ArgumentError, "new_instances requires a Class to stub" unless Class === @obj
      location = caller.first
      allocators = [:new] if allocators.empty?
      result = ExpectationRecorder.new
      allocators.each do |allocate_method|
        check_allocate_method(allocate_method)
        # HACK: Without the following lambda, Some versions of Ruby 1.9
        #       would not bind the allocate_method parameter
        #       correctly. I don't think it is necessary for recent
        #       versions.
        lambda { }
        self.flexmock_define_expectation(location, allocate_method).and_return { |*args|
          new_obj = invoke_original(allocate_method, args)
          mock = flexmock_container.flexmock(new_obj)
          block.call(mock) if block_given?
          result.apply(mock)
          new_obj
        }
      end
      result
    end

should_receive(:method_name) click to toggle source
should_receive(:method1, method2, ...)
should_receive(:meth1 => result1, :meth2 => result2, ...)

Declare that the partial mock should receive a message with the given name.

If more than one method name is given, then the mock object should expect to receive all the listed melthods. If a hash of method name/value pairs is given, then the each method will return the associated result. Any expectations applied to the result of `should_receive` will be applied to all the methods defined in the argument list.

An expectation object for the method name is returned as the result of this method. Further expectation constraints can be added by chaining to the result.

See Expectation for a list of declarators that can be used.

    # File lib/flexmock/partial_mock.rb, line 82
    def should_receive(*args)
      location = caller.first
      flexmock_define_expectation(location, *args)
    end

[Validate]

Generated with the Darkfish Rdoc Generator 2.