djcrankypants / spock

Automatically exported from code.google.com/p/spock
0 stars 0 forks source link

Spying with Spock #261

Closed GoogleCodeExporter closed 8 years ago

GoogleCodeExporter commented 8 years ago
It would be nice if Spock supported spying feature just like Mockito. 

I saw from github commits that you are already working on it, but I'm not sure 
if it is exactly what I'm looking for. 

My interpretation of the spy feature is that you can spy-mock an existing 
instance and every not mocked/stubbed method call is delegated to the existing 
instance.

My special use-case is to create a spy from the existing instance, pass it to 
the depending implementation, and then check how many times method did get 
called (Caching hit/miss test).

Original issue reported on code.google.com by denis.st...@gmail.com on 16 Jul 2012 at 9:39

GoogleCodeExporter commented 8 years ago
The latest snapshots already support this. Currently, the spied-on instance is 
created implicitly as part of the `Spy()` call:

def foo = Spy(Foo, constructorArgs: [...])

Mockito's solution of passing an existing instance to the `spy` method may seem 
nicer but in fact employs dark magic. It doesn't use the existing instance at 
all except for copying all its field values to another instance. I'd rather 
want to avoid this. Anyway, the outcome is the same.

Original comment by pnied...@gmail.com on 16 Jul 2012 at 9:47

GoogleCodeExporter commented 8 years ago
In my case spied-on instance is created via factory class using the create 
method with parameters. 

Supporting constructor only initialization could be very limiting, some objects 
may have private constructor and static method to create objects.

Could it be supported when spied-on instance is an interface implementation? 
I'm assuming in this case no fields copying is needed.

def foo = Spy(Foo, impl: fooImpl)

in this case Foo.class is required to be an interface.

Original comment by denis.st...@gmail.com on 16 Jul 2012 at 10:19

GoogleCodeExporter commented 8 years ago
With Mockito's approach, field copying is always necessary. This makes it scary 
because the test isn't actually using the provided instance. In the case where 
the instance has to be created with a factory it's probably the only way out 
though. I'll have to think whether we want to support that.

Original comment by pnied...@gmail.com on 16 Jul 2012 at 10:31

GoogleCodeExporter commented 8 years ago
We now support spying in general. If you need spying on an existing instance, 
please open another issue.

Original comment by pnied...@gmail.com on 4 Oct 2012 at 6:50