Skip to content

[Bug]: Mocking a hierarchy with a large depth doesn't build #6264

Description

@jpelcis

Description

Mocking a hierarchy with a large depth doesn't build.

Expected Behavior

Application to compile.

Actual Behavior

Application does not compile.

Steps to Reproduce

public interface IDepth1
{
    IDepth2 Inner { get; }
}

public interface IDepth2
{
    IDepth3 Inner { get; }
}

public interface IDepth3
{
    IDepth4 Inner { get; }
}

public interface IDepth4
{
    IDepth5 Inner { get; }
}

public interface IDepth5
{
}

public class Tester
{
    [Test]
    public async Task LargeHierarchy()
    {
        var mock = IDepth1.Mock();

        // If the mock type is generated then the build works
        // var mock5 = IDepth5.Mock();
    }
}

TUnit Version

1.56.0

.NET Version

.NET 10

Operating System

Windows

IDE / Test Runner

dotnet CLI (dotnet test / dotnet run)

Error Output / Stack Trace

TUnit.Mocks.SourceGenerator\TUnit.Mocks.SourceGenerator.MockGenerator\IDepth4_MockImplFactory.g.cs(19,156): error CS0400: The type or namespace name 'IDepth5MockFactory' could not be found in the global namespace (are you missing an assembly reference?)

Additional Context

I didn't debug to confirm, but this might be from MockTypeDiscovery having a max depth.

I think the design decision to not create mock types too far down the hierarchy is reasonable. I know I don't currently have a use case that requires it. The properties won't actually be used past a certain depth.

The problem is that it breaks the ability to mock the top level interface.

IDE-Specific Issue?

  • I've confirmed this issue occurs when running via dotnet test or dotnet run, not just in my IDE

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions