Isolator feature focus: Recursive Fakes

Here’s another feature Typemock Isolator has that no one else currently has: the ability to return fake objects from properties recursively (without the use of an auto mocking container for such a feat). this saved a lot of reperitive test code of creating stubs that return stubs that return some custom result.

For example, an object person has a manager which is also a person (which of course also has a manager etc..)

here’s how you can use this feature:

[Test,Isolated]
public void test()
{
    Person person = Isolate.Fake.Instance<Person>(Members.ReturnRecursiveFakes);
    Assert.IsNotNull(person.Manager.Manager.Manager.Manager);
}

This test will pass (Manager is null by default. look at the class in question below:

class Person
   {
       public string Name
       {
           get { return name; }
       }

       public Person Manager
       {
           get { return manager; }
       }

       private string name;
       private Person manager;
   }

 

Here is how you can return stub results from properties of the stubbed objects:

[Test,Isolated]
public void test()
{
    Person person = Isolate.Fake.Instance<Person>(Members.ReturnRecursiveFakes);
    Isolate.WhenCalled(() => person.Manager.Manager.Name)
               .WillReturn("roy");

    Assert.AreEqual("roy",person.Manager.Manager.Name);
}

 

Notice that:

  • By saying “ReturnRecursiveFakes” we are actually saying “I don’t care who uses this object and how from now on. It will return a fake when needed from all properties and the properties of the stubs as well.

  • We can still set stub results on the fakes on any level

 

If we wanted to only fake the Manager name and leave everything else intact we could just use a live object in the test like this:

 

[Test,Isolated]
public void test()
{
    Person person = new Person();

    Isolate.WhenCalled(() => person.Manager.Manager.Name)
               .WillReturn("roy");
    Assert.AreEqual("roy",person.Manager.Manager.Name);
}

Published Saturday, October 04, 2008 6:17 PM by RoyOsherove

Comments

Saturday, October 04, 2008 6:51 PM by Ayende Rahien

# re: Isolator feature focus: Recursive Fakes

Roy,

I like this!

Sunday, October 05, 2008 8:14 AM by Dew Drop - October 5, 2008 | Alvin Ashcraft's Morning Dew

# Dew Drop - October 5, 2008 | Alvin Ashcraft's Morning Dew

Pingback from  Dew Drop - October 5, 2008 | Alvin Ashcraft's Morning Dew

Sunday, October 05, 2008 11:56 PM by Daniel Cazzulino

# re: Isolator feature focus: Recursive Fakes

This has been in Moq's trunk for a couple weeks now (code.google.com/.../detail)

It's indeed a cool feature :)

Monday, October 06, 2008 4:51 PM by Scott Bellware

# re: Isolator feature focus: Recursive Fakes

And on the downside, this will contribute to the obscuration of Law of Demeter violations, leading to potentially lots of transient coupling and "stolen" dependencies (dependency hub in the extreme is an anti-pattern).

I've wanted this kind of thing before in the past, but I've found that the absence of direct support for train-wreck syntax and broken encapsulation in mocking frameworks was a good thing.  This kind of thing is often a tell that there's a better design waiting to be realized.

Nonetheless, nice feature when cheating is fine and reasonable.

Saturday, October 11, 2008 5:42 PM by jdn

# re: Isolator feature focus: Recursive Fakes

"Nonetheless, nice feature when cheating is fine and reasonable."

Which is often and should in fact be encouraged whenever possible.

# Type Mock embraces SharePoint | Agile Sharepoint development by 21apps and MOSS 2007 MVP Andrew Woodward

Pingback from  Type Mock embraces SharePoint | Agile Sharepoint development by 21apps and MOSS 2007 MVP Andrew Woodward