TestGen.SubstituteGenerator
1.0.2
dotnet add package TestGen.SubstituteGenerator --version 1.0.2
NuGet\Install-Package TestGen.SubstituteGenerator -Version 1.0.2
<PackageReference Include="TestGen.SubstituteGenerator" Version="1.0.2" />
<PackageVersion Include="TestGen.SubstituteGenerator" Version="1.0.2" />
<PackageReference Include="TestGen.SubstituteGenerator" />
paket add TestGen.SubstituteGenerator --version 1.0.2
#r "nuget: TestGen.SubstituteGenerator, 1.0.2"
#:package TestGen.SubstituteGenerator@1.0.2
#addin nuget:?package=TestGen.SubstituteGenerator&version=1.0.2
#tool nuget:?package=TestGen.SubstituteGenerator&version=1.0.2
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 | Versions 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. |
-
net9.0
- Microsoft.AspNetCore.Mvc.Core (>= 2.2.5)
- NSubstitute (>= 5.3.0)
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