vendor/gems/flexmock-0.8.5/lib/flexmock/partial_mock.rb
# :call-seq:
#   new_instances.should_receive(...)
#   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(...)
#


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.


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 .
def new_instances(*allocators, &block)
      fail ArgumentError, "new_instances requires a Class to stub" unless Class === @obj
      allocators = [:new] if allocators.empty?
      result = ExpectationRecorder.new
      allocators.each do |allocate_method|
        lambda { } 
        self.should_receive(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
def new_instances ( * allocators , & block ) fail ArgumentError , "new_instances requires a Class to stub" unless Class === @obj allocators = [ :new ] if allocators . empty? result = ExpectationRecorder . new allocators . each do | allocate_method | lambda { } self . should_receive ( 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
:call-seq:
new_instances.should_receive(...)
new_instances { |instance|  instance.should_receive(...) }
: call - seq : new_instances . should_receive ( ... ) new_instances { |instance| instance . should_receive ( ... ) }
# HACK: Without the following lambda, Ruby 1.9 will not bind # the allocate_method parameter correctly.
allocators
block