TestGen.SubstituteGenerator 1.0.2

dotnet add package TestGen.SubstituteGenerator --version 1.0.2
                    
NuGet\Install-Package TestGen.SubstituteGenerator -Version 1.0.2
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="TestGen.SubstituteGenerator" Version="1.0.2" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="TestGen.SubstituteGenerator" Version="1.0.2" />
                    
Directory.Packages.props
<PackageReference Include="TestGen.SubstituteGenerator" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add TestGen.SubstituteGenerator --version 1.0.2
                    
#r "nuget: TestGen.SubstituteGenerator, 1.0.2"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package TestGen.SubstituteGenerator@1.0.2
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=TestGen.SubstituteGenerator&version=1.0.2
                    
Install as a Cake Addin
#tool nuget:?package=TestGen.SubstituteGenerator&version=1.0.2
                    
Install as a Cake Tool

TestGen.SubstituteGenerator

A powerful substitute generator for creating sociable test classes with deep dependency resolution using NSubstitute.

๐Ÿš€ Features

  • Deep Dependency Resolution: Recursively analyzes the entire dependency tree (Controller โ†’ Manager โ†’ Service โ†’ Repository)
  • Generic Type Support: Full support for generic interfaces like IRepository<T>, IService<TIn, TOut>
  • Accurate Mock Setup: Analyzes actual interface methods to generate precise SetupDefaults() configurations
  • Smart Return Values: Intelligent default return value generation for async methods, collections, primitives, and complex types
  • File Generation: Creates substitute class files directly to specified directories
  • C# Naming Conventions: Proper PascalCase property naming following C# standards

๐Ÿ“ฆ Installation

dotnet add package TestGen.SubstituteGenerator

๐ŸŽฏ Usage

Basic Usage

using TestGen;
using YourApp.Controllers;

// Generate substitute class content
var substituteCode = SubstituteGenerator.GenerateSubstituteClass(typeof(UserController));

// Or create the file directly
SubstituteGenerator.GenerateSubstituteClass(typeof(UserController), "./Tests/Substitutes");

Example Generated Substitute

For a UserController with dependencies on IUserService, IEmailService, and ILoggerService, the generator creates:

public class UserControllerSubstitute
{
    // All dependencies across the entire tree
    public IUserService UserService { get; }
    public IEmailService EmailService { get; }
    public ILoggerService LoggerService { get; }
    public IUserRepository UserRepository { get; }  // From UserService dependencies
    public IAuditService AuditService { get; }      // From UserService dependencies
    
    public UserController Controller { get; }

    public UserControllerSubstitute()
    {
        // All mocks initialized
        UserService = Substitute.For<IUserService>();
        EmailService = Substitute.For<IEmailService>();
        // ... etc
        
        Controller = new UserController(UserService, EmailService, LoggerService);
    }

    public void SetupDefaults()
    {
        // Accurate method-specific setups based on actual interface analysis
        UserService.GetUserByIdAsync(Arg.Any<int>()).Returns(Task.FromResult((User?)null));
        EmailService.ValidateEmailAsync(Arg.Any<string>()).Returns(Task.FromResult(true));
        // ... etc
    }
}

๐Ÿงช Sociable Testing

The generator enables true sociable testing where:

โœ… Business logic flows through all application layers
โœ… Only external dependencies (database, network, etc.) are mocked
โœ… Fast, reliable test execution without external calls
โœ… Comprehensive coverage of dependency interactions

๐Ÿ”ง Advanced Features

Generic Type Support

// Handles generic interfaces automatically
IGenericRepository<User> โ†’ GenericRepositoryUser
IDataService<string, int> โ†’ DataServiceStringInt
IValidator<CreateUserRequest> โ†’ ValidatorCreateUserRequest

Smart Default Generation

// Async methods
Task<User> โ†’ Task.FromResult(new User())
Task<bool> โ†’ Task.FromResult(true)
Task โ†’ Task.CompletedTask

// Collections
IEnumerable<User> โ†’ new List<User>()
Task<IList<Product>> โ†’ Task.FromResult(new List<Product>())

// Primitives
bool โ†’ true, int โ†’ 0, string โ†’ ""

๐Ÿ“– Documentation

Visit our GitHub repository for full documentation, examples, and contribution guidelines.

๐Ÿ“„ License

This project is licensed under the MIT License.

Product Compatible and additional computed target framework versions.
.NET net9.0 is compatible.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 was computed.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
1.0.2 77 8/1/2025

v1.0.0:
   - Deep dependency resolution with recursive tree analysis
   - Support for generic interfaces and types
   - Accurate SetupDefaults method generation based on interface analysis
   - Smart return value generation for async methods, collections, and primitives
   - Proper C# naming conventions for generated properties
   - File creation capabilities with directory support