Isolator Feature Focus: Duck Typing and Isolate.Swap

Typemock Isolator 5.1.1 has been released, and this release brings with it some awesome (seriously) features that are unique fro any other framework I've seen.

a good overview of features can be found here.

The Isolator Swap feature allows swapping calls between real and fake objects (kind like redirects) so that any relevant calls made against the real object will be redirected and invoked on the fake object.

Here's how you use it:

image

Unlike doing a standard "WhenCalled()" on some object method and telling what its custom behavior will be, "Swapping"  objects redirects all relevant calls(I'll explain what "relevant" means in a second) to the fake object which you have created.

 Duck Typing Awesomeness

Another cool thing about it is that "Duck" and "Dog" don't have to have a shared interface or base class. The "Swap" feature will redirect a method call if it exists on the "fake" object, but if it does not, it will invoke the original object. This is one interpretation of what's called "Duck Typing".

The "CallsOn" and "WithCallsTo" methods take an object type, so you can send in anything you want.  if a method on the fake object matches the signature and name of a called method on the real object, the fake method will be invoked.

Published Sunday, November 02, 2008 7:25 AM by RoyOsherove

Comments

# Reflective Perspective - Chris Alcock » The Morning Brew #214

Pingback from  Reflective Perspective - Chris Alcock  » The Morning Brew #214

Monday, November 03, 2008 5:55 AM by Colin Jack

# re: Isolator Feature Focus: Duck Typing and Isolate.Swap

Struggling to find a reason that you would use this in a real situation, especially as it will obfuscate your tests (making readers work out which object the method call will be on).

Monday, November 03, 2008 8:13 AM by RoyOsherove

# re: Isolator Feature Focus: Duck Typing and Isolate.Swap

Colin,

it can help immensely to achieve simpler code in some cases. You want to have your own stub class that you manually created, that will be used throughout your test projects.

instead of creating lots of "whenCalled" on some class and returning fake values, you simply swap a class implementation with another.

you can think about it as an evolution of using hand coded stubs, without needing dependency injection techniques.

the suck typing feature allows you to only override what you care about, or to have a class that overrides methods for many different classes at the same time, in one place.

Monday, November 03, 2008 10:27 AM by Colin Jack

# re: Isolator Feature Focus: Duck Typing and Isolate.Swap

@Roy

Get you now, yeah that makes sense. Still think it seems tricksy enough that it would throw most people. Guess I'd like the option to be explicit:

Isolate.Swap.CallTo(x => x.Talk).On(dog).WithCallTo(duck);

You could come back and say thats being too explicit and could result in fragile overspecified tests, and I agree...but I would have thought the choice would be useful.

Monday, November 03, 2008 4:32 PM by Doron

# re: Isolator Feature Focus: Duck Typing and Isolate.Swap

@Colin

This is a good idea - we are actually planning to enable doing this through the WhenCalled() entry point:

Isolate.WhenCalled(() => x.Talk()).Do(() => y.Walk()); // or something along these lines.

This in addition to the entire object duck type swapping Roy showed above - it's a cool tool for the situations in which you would like to substitute an entire object.

@Roy

We call the feature Duck-typing, but Suck-typing sounds like something we could get on the product backlog for an upcoming release :)