New to Telerik JustMockStart a free 30-day trial

RecursiveLoose

Updated on Jan 27, 2026

RecursiveLoose mocks will return new mocks (with Behavior.RecursiveLoose) for all members/functions of the mocked type. However, as there are types that cannot be mocked (string, int, etc.), RecursiveLoose mocks will return default value for all value type members and empty, non-null stubs for strings. Also, a non-null empty collection will be returned for collection return types (e.g. array or IEnumerable) .

RecursiveLoose mocks are the same as Loose mocks, but with one difference: They will automatically generate empty stubs for all mock members, on all levels. This saves time in manually arranging or initializing all the mock prerequisites (code examples). Further, you can change the per-arranged behavior by defining new expectations, as described in the JustMock API Basics articles.

RecursiveLoose is the default behavior when creating mocks.

Syntax

As this is every mocks default behavior, its enough to write - Mock.Create<T>();. The RecursiveLoose behavior will be applied automatically.

To explicitly set Behavior.RecursiveLoose use Mock.Create<T>(Behavior.RecursiveLoose);

Examples

The Difference Between RecursiveLoose and Loose Mocks

To explain what differs between both behaviors, let's look at the following interface:

C#
public interface IFirst
{
    ISecond Second { get; set; }
}
public interface ISecond
{
    IThird Third { get; set; }
}

public interface IThird
{
    IFourth Fourth { get; set; }
    Task<IFourth> GetFourthAsync();
}

public interface IFourth
{
    string DoSomething();
}

Here, we have a number of nested properties. If we try to call a low level, non-arranged function from a Loose mock, the test will throw a NullReferenceException. This is shown in the next example:

C#
[TestMethod]
[ExpectedException(typeof(NullReferenceException))]
public void ShouldAssertAgainstNonArrangedChainCallInLooseMock()
{
    //Arrange
    var foo = Mock.Create<IFirst>(Behavior.Loose);

    //Act
    foo.Second.Third.Fourth.DoSomething(); // This will throw a NullReferenceException
}

The exception is thrown right after the initialization of the Second property, as it returns the default reference type value (null). To avoid this in a Loose mock, we need to arrange the method chain, like this:

C#
[TestMethod]
public void ShouldAssertAgainstArrangedChainCallInLooseMock()
{
    //Arrange
    var foo = Mock.Create<IFirst>(Behavior.Loose);

    Mock.Arrange(() => foo.Second.Third.Fourth.DoSomething()).Returns(String.Empty);

    //Act
    var actual = foo.Second.Third.Fourth.DoSomething();

    // Assert
    Assert.IsNotNull(actual);
}

Using RecursiveLoose mocks, we are capable of writing the following:

C#
[TestMethod]
public void ShouldAssertAgainstNonArrangedChainCallInRecursiveLooseMock()
{
    //Arrange
    var foo = Mock.Create<IFirst>(Behavior.RecursiveLoose); // This equals to: Mock.Create<IFirst>();

    //Act
    var actual = foo.Second.Third.Fourth.DoSomething();

    // Assert
    Assert.IsNotNull(actual);
}

Notice that no additional arrangements are needed for the test to pass.

Using RecursiveLoose Mocks in Your Tests

Assume we have the next system under test:

C#
public class Product
{

}

public class Order
{
    public List<Product> Products
    {
        get { return products; }
    }

    private List<Product> products;
}

public class OrderRepository
{
    public Order Order
    {
        get { return order; }
    }

    private Order order;
}

public class ClassUnderTest
{
    public OrderRepository Repo
    {
        get { return new OrderRepository(); }
    }

    public string MethodUnderTest()
    {
        if (Repo.Order.Products != null)
        {
            return "Pass";
        }

        return "Fail";
    }
}

To enter the If statement of the MethodUnderTest(), we simply need to pass a RecursiveLoose mock, like this:

C#
[TestMethod]
public void ShouldReturnRecursiveMock()
{
    var cut = new ClassUnderTest();

    // Arrange
    Mock.Arrange(() => cut.Repo).Returns(Mock.Create<OrderRepository>());

    // Act
    var actual = cut.MethodUnderTest();

    // Assert
    Assert.AreEqual("Pass", actual);
}

RecursiveLoose Mocks in Async Tests

The RecursiveLoose behavior automatically intercepts the return types in asynchronous calls and returns a mock object:

C#
[TestMethod]
public async Task ShouldAssertAgainstNonArrangedChainCallInRecursiveLooseMockAsync()
{
    //Arrange
    var foo = Mock.Create<IFirst>(Behavior.RecursiveLoose); // This equals to: Mock.Create<IFirst>();

    //Act
    var actual = await foo.Second.Third.GetFourthAsync();

    // Assert
    Assert.IsNotNull(actual.DoSomething());
}

In some scenarios returning mock objects on asynchronous calls might lead to undesired behavior during acting phase, consider the cases when the return type is a system type. The automatic return type interception could be disabled by explicitly using NotIntercept in the following way:

C#
[TestMethod]
[ExpectedException(typeof(NotImplementedException))]
public async Task ShouldThrowAgainstNonArrangedChainCallInRecursiveLooseMockAsync()
{
    //Arrange
    Mock.NotIntercept<IFourth>();
    var foo = Mock.Create<IFirst>(Behavior.RecursiveLoose);

    //Act
    var actual = await foo.Second.Third.GetFourthAsync();

    // Assert
    Assert.IsNull(actual.DoSomething());
}

See Also