Table of Contents
Warning

The following document was generated by AI and HAS NOT YET been reviewed.

Troubleshooting

This section covers common error messages, performance considerations, known limitations, and solutions to help you resolve issues when using TDoubles.

Common Error Messages and Solutions

MOCK001: Mock class must be partial

Error Message: The class 'YourMockClass' decorated with [Mock] attribute must be declared as partial

Cause: The mock class is missing the partial keyword, which is required for the source generator to add the implementation.

Solution:

// ❌ Incorrect - Missing partial keyword
[Mock(typeof(IUserService))]
class UserServiceMock
{
}

// ✅ Correct - Include partial keyword
[Mock(typeof(IUserService))]
partial class UserServiceMock
{
}

MOCK002: Invalid generic type argument count

Error Message: The mock class 'YourMockClass' must have {expected} generic type arguments to match target type '{TargetType}'

Cause: The number of generic type parameters on the mock class doesn't match the target type.

Solution:

// Target type with 2 generic parameters
public class GenericService<T, U> { }

// ❌ Incorrect - Wrong number of type parameters
[Mock(typeof(GenericService<,>))]
partial class GenericServiceMock<T>
{
}

// ✅ Correct - Matching number of type parameters
[Mock(typeof(GenericService<,>))]
partial class GenericServiceMock<T, U>
{
}

MOCK003: Invalid Mock attribute usage

Error Message: The [Mock] attribute on class 'YourMockClass' must specify a target type using typeof(TargetType)

Cause: The Mock attribute is missing the required typeof() parameter or has an invalid parameter.

Solution:

// ❌ Incorrect - Missing typeof parameter
[Mock]
partial class UserServiceMock
{
}

// ❌ Incorrect - Invalid parameter type
[Mock("IUserService")]
partial class UserServiceMock
{
}

// ✅ Correct - Use typeof() with the target type
[Mock(typeof(IUserService))]
partial class UserServiceMock
{
}

MOCK004: Circular mock dependency detected

Error Message: Circular dependency detected in mock generation for type '{TargetType}'. Mock types cannot reference each other in a circular manner.

Cause: Two or more mock types reference each other, creating a circular dependency that would cause infinite recursion during generation.

Solution: Redesign your mock structure to avoid circular references:

// ❌ Problematic - Circular dependency
public class ServiceA
{
    public ServiceB ServiceB { get; set; }
}

public class ServiceB  
{
    public ServiceA ServiceA { get; set; }
}

[Mock(typeof(ServiceA))]
partial class ServiceAMock { } // This references ServiceB

[Mock(typeof(ServiceB))]
partial class ServiceBMock { } // This references ServiceA - CIRCULAR!

// ✅ Solution - Use composition or break the circular reference
public interface IServiceA
{
    void DoWork();
}

public interface IServiceB
{
    void DoWork();
}

public class ServiceA : IServiceA
{
    private readonly IServiceB _serviceB;
    public ServiceA(IServiceB serviceB) => _serviceB = serviceB;
    public void DoWork() => _serviceB.DoWork();
}

[Mock(typeof(IServiceA))]
partial class ServiceAMock { }

[Mock(typeof(IServiceB))]
partial class ServiceBMock { }

MOCK005: Invalid mock target type

Error Message: The target type '{TargetType}' specified in [Mock] attribute is not supported for mocking. Supported types are classes, structs, interfaces, records, and record structs.

Cause: You're trying to mock an unsupported type such as enums, delegates, or primitive types.

Solution: Only mock supported types:

// ❌ Unsupported types
public enum Status { Active, Inactive }
public delegate void MyDelegate();

[Mock(typeof(Status))]     // Error - enums not supported
partial class StatusMock { }

[Mock(typeof(MyDelegate))] // Error - delegates not supported  
partial class DelegateMock { }

// ✅ Supported types
public interface IService { }
public class Service { }
public record UserRecord(string Name);
public struct Point { public int X, Y; }

[Mock(typeof(IService))]
partial class ServiceMock { }

[Mock(typeof(Service))]
partial class ServiceClassMock { }

[Mock(typeof(UserRecord))]
partial class UserRecordMock { }

[Mock(typeof(Point))]
partial class PointMock { }