CC.Generators
1.0.0
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
<PackageReference Include="CC.Generators" Version="1.0.0" />
<PackageVersion Include="CC.Generators" Version="1.0.0" />
<PackageReference Include="CC.Generators" />
paket add CC.Generators --version 1.0.0
#r "nuget: CC.Generators, 1.0.0"
#:package CC.Generators@1.0.0
#addin nuget:?package=CC.Generators&version=1.0.0
#tool nuget:?package=CC.Generators&version=1.0.0
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);
}
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 |