CC.Generators 1.0.0

There is a newer version of this package available.
See the version list below for details.
dotnet add package CC.Generators --version 1.0.0
                    
NuGet\Install-Package CC.Generators -Version 1.0.0
                    
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="CC.Generators" Version="1.0.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="CC.Generators" Version="1.0.0" />
                    
Directory.Packages.props
<PackageReference Include="CC.Generators" />
                    
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 CC.Generators --version 1.0.0
                    
#r "nuget: CC.Generators, 1.0.0"
                    
#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 CC.Generators@1.0.0
                    
#: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=CC.Generators&version=1.0.0
                    
Install as a Cake Addin
#tool nuget:?package=CC.Generators&version=1.0.0
                    
Install as a Cake Tool

CC.Generators

Purpose

When unit testing, we often need to create instances of classes that have multiple dependencies. This can lead to a lot of boilerplate code in our test classes, as we have to manually create mocks or stubs for each dependency. The CC.Generators library helps to reduce this boilerplate code by automatically generating methods that create instances of classes with their dependencies.

Example

Let's say we have the following class with several dependencies:

public class CustomerService : ICustomerService
{
    private readonly IRepository _repository;
    private readonly IEmailService _emailService;
    private readonly ILogger _logger;
    public CustomerService(IRepository repository, IEmailService emailService, ILogger logger)
    {
        _repository = repository;
        _emailService = emailService;
        _logger = logger;
    }
}

In our test class, we want to create an instance of CustomerService without having to manually create all the dependencies. We can use the CC.Generators library to automatically generate a method that creates an instance of our class under test for us just by adding the Creator attribute to our test class.

[CC.Generators.Creator(Target = typeof(CustomerService))]
[TestFixture]
public partial class CustomerServiceTests
{
    [Test]
    public void VerifyCustomerService()
    {
        var service = CreateCustomerService();
        Assert.That(service, Is.Not.Null);
    }
}

That will cause the generation of a private method, CreateCustomerService, responsible for creating an instance of CustomerService with all its dependencies. The generated method will look like this:

private static ICustomerService CreateCustomerService(MockBehavior defaultBehavior = MockBehavior.Loose,
    IRepository repository = null,
    IEmailService emailService = null, 
    ILogger logger = null)
{
    return new CustomerService(
        repository ?? new Mock<IRepository>(defaultBehavior).Object,
        emailService ?? new Mock<IEmailService>(defaultBehavior).Object,
        logger ?? new Mock<ILogger>(defaultBehavior).Object
    );
}

The CreateCustomerService method will create a new instance of CustomerService with all its dependencies. If you want to use a specific mock for any of the dependencies, you can pass it as a named argument to the method. If you don't pass a mock, a new mock will be created for that dependency. You can also specify the MockBehavior for the generated mocks by passing it as an argument to the CreateCustomerService method.

[Test]
public void VerifyCustomerServiceWithSpecificMocks()
{
    // Setup the only mock we need to customize for this particular test
    var repositoryMock = new Mock<IRepository>(MockBehavior.Strict);
    reositoryMock
        .Setup(x => x.GetCustomerById(It.IsAny<int>()))
        .Returns(new Customer());

    // Using named arguments to pass the specific mock, the other dependencies will be created as new mocks with default behavior MockBehavior.Loose
    var service = CreateCustomerService(repository: repositoryMock.Object);

    Assert.That(service, Is.Not.Null);
}
There are no supported framework assets in this package.

Learn more about Target Frameworks and .NET Standard.

  • .NETStandard 2.0

    • No dependencies.

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.1.0 171 5/22/2025
1.0.0 158 5/8/2025
0.0.3-alpha 142 5/7/2025
0.0.2-alpha 140 5/5/2025
0.0.1-alpha 247 5/4/2025