---
title: "Isolator feature focus: Recursive Fakes"
url: "https://osherove.com/blog/2008/10/4/isolator-feature-focus-recursive-fakes.md"
canonical: "https://osherove.com/blog/2008/10/4/isolator-feature-focus-recursive-fakes.html"
published: "2008-10-04T15:17:05"
updated: "2008-10-04T15:17:05"
tags: [".NET", "Agile", "FeatureFocus", "Typemock", "Unit Testing"]
author: "Roy Osherove"
description: "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…"
usage: "Licensed under CC BY 4.0 — reuse, quote and adapt with attribution (Roy Osherove, link to the original page)."
license: "https://creativecommons.org/licenses/by/4.0/"
---

# Isolator feature focus: Recursive Fakes

By Roy Osherove October 4, 2008 In [.NET](https://osherove.com/blog/category/net), [Agile](https://osherove.com/blog/category/agile), [FeatureFocus](https://osherove.com/blog/category/featurefocus), [Typemock](https://osherove.com/blog/category/typemock), [Unit Testing](https://osherove.com/blog/category/unit-testing)

**Historical article** · Originally published October 4, 2008; technical details may be outdated.

Here’s another feature [Typemock Isolator](http://www.typemock.com/) 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);
}

## Lessons on this topic

- [Recursive Fakes](https://courses.osherove.com/courses/tdd-in-net-in-hebrew/recursive-fakes/) · TDD in .NET in Hebrew
- [Testing Legacy Code with Typemock Isolator Deep Dive](https://courses.osherove.com/courses/the-art-of-unit-testing-and-tdd-master-class-in-net/testing-legacy-code-with-typemock-isolator-deep-dive/) · The Art of Unit Testing & TDD Master Class in .NET
- [Isolation Frameworks](https://courses.osherove.com/courses/the-art-of-unit-testing-and-tdd-master-class-in-net/isolation-frameworks/) · The Art of Unit Testing & TDD Master Class in .NET
- [Test Review Practices](https://courses.osherove.com/courses/the-art-of-unit-testing-and-tdd-master-class-in-net/test-review-practices/) · The Art of Unit Testing & TDD Master Class in .NET

## More in .NET

[All posts in .NET](https://osherove.com/blog/category/net)

- Oct 6, 2008

    ### [Graphic Designer Wanted (and found)](https://osherove.com/blog/2008/10/6/graphic-designer-wanted-and-found.html)

- Oct 6, 2008

    ### [Deadlock Challenge](https://osherove.com/blog/2008/10/6/deadlock-challenge.html)

- Oct 4, 2008

    ### [BDD: Behavior vs. Spec Frameworks](https://osherove.com/blog/2008/10/4/bdd-behavior-vs-spec-frameworks.html)

- Oct 3, 2008

    ### [Agile Testing tools List](https://osherove.com/blog/2008/10/3/agile-testing-tools-list.html)


---

## About the author

**Roy Osherove**: Senior Solutions Architect at AWS working on AI for production and AI-driven development; author of The Art of Unit Testing and Elastic Leadership, with about 20 years of consulting and training on unit testing, TDD, team leadership and continuous delivery.

### Sites

- [RobotPaper](https://robotpaper.ai/): This blog: AI patterns and anti-patterns, AI and coding agents, and engineering leadership.
- [osherove.com](https://osherove.com): Roy's personal site. Author of The Art of Unit Testing and Elastic Leadership; consulting and training on unit testing, TDD, team leadership and continuous delivery, plus about 20 years of blog posts.
- [The Art of Unit Testing](https://www.artofunittesting.com): The Art of Unit Testing, 3rd Edition (Manning): readable, maintainable, trustworthy unit tests, plus TDD workshops in C#, JavaScript, Java and Kotlin.
- [5whys.com](https://www.5whys.com): Roy's writing on software team leadership: self-organizing teams, surviving chaos, learning mode and the Elastic Leadership model.
- [Pipeline Driven](https://pipelinedriven.org): Continuous delivery and DevOps practices: build patterns, pipeline-friendly testing, DevOps metrics and enterprise DevOps.
- [Elastic Leadership](https://www.elasticleadership.com): Elastic Leadership, Roy's framework for software team leaders (survival, learning and self-organizing modes).

### Social

- X: https://x.com/royosherove
- LinkedIn: https://www.linkedin.com/in/osherove/

### Contact

- Contact form: https://forms.gle/jQSx5KoXvrVA1JCK7

### License

Licensed under CC BY 4.0 — reuse, quote and adapt with attribution (Roy Osherove, link to the original page). License: https://creativecommons.org/licenses/by/4.0/
