Closed mcmire closed 11 years ago
Also DoubleDefinitionCreate still aliases #instance_of to #any_instance_of, not sure if that's on purpose or what...
So as far as I can tell, there are two methods because, well, history. But originally there were two use cases:
new_instance_of(Foo, &block)
, when Foo.new is called the new_instance_of
block is yielded with the Foo object which lets you keep the existing .new method but then double inject instances. With any_instance_of(Foo, &block)
, the block is immediately yielded with a proxy object (PrototypeSubject) and it is up to you to wrap this object with stub or mock (when you do so RR specially handles wrapping a PrototypeSubject object); this is useful if all you want to do is double inject instance methods which are not the initializer.So, the state of things now is this: we have #any_instance_of which still exists, and then also #instance_of aka #all_instances_of. From the code it looks like #any_instance_of really isn't doing anything anymore -- RR::DoubleDefinitions::DoubleInjections::AnyInstanceOf.call is only called in one place, and without any arguments, all it does is yield the given class to the block. I think it's intended to be called like this:
any_instance_of(Foo, :bar => lambda { 42 })
Whereas #instance_of is intended to be called like this:
instance_of(Foo).bar { 42 }
instance_of(Foo) do
bar { 42 }
end
We probably just need to deprecate #any_instance_of...
Just to clarify, DoubleInjection operates on methods within a class. The difference between the AnyInstanceOf and Instance double injection strategies is that Instance gives the metaclass of an object to DoubleInjection whereas AnyInstanceOf gives a class itself.
Its too bad that #new_instance_of
was removed. It allows some use cases that the other forms do not allow form. Specifically, it gave you access to the instance from within the stubs, so that you could perform an action or return a value from the stub that depended on the instance.
They both kind of seem to be doing the same thing in code. What do they do? Is there a need to keep both around?