class 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

method_definitions[R]
mock[R]

Public Class Methods

make_proxy_for(obj, container, name, safe_mode) click to toggle source

Make a partial mock proxy and install it on the target obj.

# File lib/flexmock/partial_mock.rb, line 70
def self.make_proxy_for(obj, container, name, safe_mode)
  name ||= "flexmock(#{obj.class.to_s})"
  if !obj.instance_variable_defined?("@flexmock_proxy")
    proxy_box = obj.instance_variable_set("@flexmock_proxy", ProxyBox.new)
  else
    proxy_box = obj.instance_variable_get("@flexmock_proxy")
  end

  if proxy_box.container != container
    if !proxy_box.empty?
      parent_proxy, _ = proxy_box.proxy
      parent_mock = parent_proxy.mock
    end

    mock  = FlexMock.new(name, container, parent: parent_mock)
    proxy = PartialMockProxy.new(obj, mock, safe_mode, parent: parent_proxy)
    proxy_box.push(proxy, container)
  end
  proxy_box.proxy
end
new(obj, mock, safe_mode, parent: nil) click to toggle source

Initialize a PartialMockProxy object.

# File lib/flexmock/partial_mock.rb, line 105
def initialize(obj, mock, safe_mode, parent: nil)
  @obj = obj
  @mock = mock
  @proxy_definition_module = nil
  @initialize_override = nil

  if parent
    @method_definitions = parent.method_definitions.dup
  else
    @method_definitions = {}
  end

  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 209
def add_mock_method(method_name)
  stow_existing_definition(method_name)
  proxy_module_eval do
    define_method(method_name) { |*args, &block|
      proxy = __flexmock_proxy or
        fail "Missing FlexMock proxy " +
             "(for method_name=#{method_name.inspect}, self=\#{self})"
      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 54
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 380
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 365
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 355
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 371
def flexmock_container=(container)
end
flexmock_define_expectation(location, *args) click to toggle source
# File lib/flexmock/partial_mock.rb, line 189
def flexmock_define_expectation(location, *args)
  EXP_BUILDER.parse_should_args(self, args) do |method_name|
    if !has_proxied_method?(method_name) && !has_original_method?(method_name)
      hide_existing_method(method_name)
    end
    ex = @mock.flexmock_define_expectation(location, method_name)
    if FlexMock.partials_verify_signatures
      if existing_method = @method_definitions[method_name]
        ex.with_signature_matching(existing_method)
      end
    end
    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 375
def flexmock_expectations_for(method_name)
  @mock.flexmock_expectations_for(method_name)
end
flexmock_find_expectation(*args) click to toggle source
# File lib/flexmock/partial_mock.rb, line 205
def flexmock_find_expectation(*args)
  @mock.flexmock_find_expectation(*args)
end
flexmock_get() click to toggle source

Get the mock object for the partial mock.

# File lib/flexmock/partial_mock.rb, line 128
def flexmock_get
  @mock
end
flexmock_invoke_original(method, args) click to toggle source

Invoke the original definition of method on the object supported by the stub.

# File lib/flexmock/partial_mock.rb, line 318
def flexmock_invoke_original(method, args)
  if original_method = @method_definitions[method]
    if Proc === args.last
      block = args.last
      args = args[0..-2]
    end
    original_method.bind(@obj).call(*args, &block)
  else
    @obj.__send__(:method_missing, method, *args, &block)
  end
end
flexmock_received?(*args) click to toggle source

Forward to the mock

# File lib/flexmock/partial_mock.rb, line 360
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 337
def flexmock_teardown
  if ! detached?
    initialize_stub_remove
    proxy_module_eval do
      methods = instance_methods(false).to_a
      methods.each do |m|
        remove_method m
      end
    end
    if @obj.instance_variable_defined?(:@flexmock_proxy) &&
        (box = @obj.instance_variable_get(:@flexmock_proxy))
      box.pop
    end
    @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 332
def flexmock_verify
  @mock.flexmock_verify
end
has_original_method?(m) click to toggle source

Whether the given method's original definition has been stored

# File lib/flexmock/partial_mock.rb, line 179
def has_original_method?(m)
  @method_definitions.has_key?(m)
end
has_proxied_method?(m) click to toggle source

Whether the given method is already being proxied

# File lib/flexmock/partial_mock.rb, line 184
def has_proxied_method?(m)
  @proxy_definition_module &&
      @proxy_definition_module.method_defined?(m)
end
initialize_stub(recorder, expectations_block) click to toggle source

Stubs the initialize method on a class

Calls superclass method
# File lib/flexmock/partial_mock.rb, line 261
def initialize_stub(recorder, expectations_block)
  if !@initialize_override
    expectation_blocks    = @initialize_expectation_blocks = Array.new
    expectation_recorders = @initialize_expectation_recorders = Array.new
    @initialize_override = Module.new do
      define_method :initialize do |*args, &block|
        if self.class.respond_to?(:__flexmock_proxy) && (mock = self.class.__flexmock_proxy)
          container = mock.flexmock_container
          mock = container.flexmock(self)
          expectation_blocks.each do |b|
            b.call(mock)
          end
          expectation_recorders.each do |r|
            r.apply(mock)
          end
        end
        super(*args, &block)
      end
    end
    override = @initialize_override
    @obj.class_eval { prepend override }
  end
  if expectations_block
    @initialize_expectation_blocks    << expectations_block
  end
  @initialize_expectation_recorders << recorder
end
initialize_stub?() click to toggle source
# File lib/flexmock/partial_mock.rb, line 289
def initialize_stub?
  !!@initialize_override
end
initialize_stub_remove() click to toggle source
# File lib/flexmock/partial_mock.rb, line 293
def initialize_stub_remove
  if initialize_stub?
    @initialize_expectation_blocks.clear
    @initialize_expectation_recorders.clear
  end
end
invoke_original(m, *args, &block) click to toggle source

Invoke the original of a mocked method

Usually called in a and_return statement

# File lib/flexmock/partial_mock.rb, line 171
def invoke_original(m, *args, &block)
  if block
    args << block
  end
  flexmock_invoke_original(m, args)
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 239
def new_instances(*allocators, &block)
  fail ArgumentError, "new_instances requires a Class to stub" unless
    Class === @obj
  location = caller
  allocators = [:initialize] if allocators.empty?

  expectation_recorder = ExpectationRecorder.new

  if allocators.delete(:initialize)
    initialize_stub(expectation_recorder, block)
  end

  allocators.each do |allocate_method|
    flexmock_define_expectation(location, allocate_method).and_return { |*args|
      create_new_mocked_object(
        allocate_method, args, expectation_recorder, block)
    }
  end
  expectation_recorder
end
pop_flexmock_container() click to toggle source
# File lib/flexmock/partial_mock.rb, line 136
def pop_flexmock_container
  @mock.pop_flexmock_container
end
push_flexmock_container(container) click to toggle source
# File lib/flexmock/partial_mock.rb, line 132
def push_flexmock_container(container)
  @mock.push_flexmock_container(container)
end
should_expect(*args) { |recorder| ... } click to toggle source
# File lib/flexmock/partial_mock.rb, line 164
def should_expect(*args)
  yield Recorder.new(self)
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 160
def should_receive(*args)
  flexmock_define_expectation(caller, *args)
end

Private Instance Methods

create_new_mocked_object(allocate_method, args, recorder, block) click to toggle source

Create a new mocked object.

The mocked object is created using the following steps: (1) Allocate with the original allocation method (and args) (2) Pass to the block for custom configuration. (3) Apply any recorded expecations

# File lib/flexmock/partial_mock.rb, line 307
def create_new_mocked_object(allocate_method, args, recorder, block)
  new_obj = flexmock_invoke_original(allocate_method, args)
  mock = flexmock_container.flexmock(new_obj)
  block.call(mock) unless block.nil?
  recorder.apply(mock)
  new_obj
end
define_proxy_method(method_name) click to toggle source

Define a proxy method that forwards to our mock object. The proxy method is defined as a singleton method on the object being mocked.

# File lib/flexmock/partial_mock.rb, line 438
    def define_proxy_method(method_name)
      if method_name =~ /=$/
        proxy_module_eval do
          define_method(method_name) do |*args, &block|
            __flexmock_proxy.mock.__send__(method_name, *args, &block)
          end
        end
      else
        proxy_module_eval <<-EOD
          def #{method_name}(*args, &block)
            FlexMock.verify_mocking_allowed!
            __flexmock_proxy.mock.#{method_name}(*args, &block)
          end
        EOD
      end
    end
detached?() click to toggle source

Have we been detached from the existing object?

# File lib/flexmock/partial_mock.rb, line 456
def detached?
  @obj.nil?
end
hide_existing_method(method_name) click to toggle source

Hide the existing method definition with a singleton defintion that proxies to our mock object. If the current definition is a singleton, we need to record the definition and remove it before creating our own singleton method. If the current definition is not a singleton, all we need to do is override it with our own singleton.

# File lib/flexmock/partial_mock.rb, line 419
def hide_existing_method(method_name)
  existing_method = stow_existing_definition(method_name)
  define_proxy_method(method_name)
  existing_method
end
proxy_module_eval(*args, &block) click to toggle source

Evaluate a block into the module we use to define the proxy methods

# File lib/flexmock/partial_mock.rb, line 398
def proxy_module_eval(*args, &block)
  if !@proxy_definition_module
    obj = @obj
    @proxy_definition_module = m = Module.new do
      define_method("__flexmock_proxy") do
        if box = obj.instance_variable_get(:@flexmock_proxy)
          box.proxy
        end
      end
    end
    target_class_eval { prepend m }
  end
  @proxy_definition_module.class_eval(*args, &block)
end
stow_existing_definition(method_name) click to toggle source

Stow the existing method definition so that it can be recovered later.

# File lib/flexmock/partial_mock.rb, line 427
def stow_existing_definition(method_name)
  if !@method_definitions.has_key?(method_name)
    @method_definitions[method_name] = target_class_eval { instance_method(method_name) }
  end
  @method_definitions[method_name]
rescue NameError
end
target_class_eval(*args, &block) click to toggle source

Evaluate a block (or string) in the context of the singleton class of the target partial object.

# File lib/flexmock/partial_mock.rb, line 393
def target_class_eval(*args, &block)
  target_singleton_class.class_eval(*args, &block)
end
target_singleton_class() click to toggle source

The singleton class of the object.

# File lib/flexmock/partial_mock.rb, line 387
def target_singleton_class
  @obj.singleton_class
end