Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/owen-krueger/autofixture.community.customizations
Useful customizations for AutoFixture
https://github.com/owen-krueger/autofixture.community.customizations
autofixture csharp dateonly net6 net7 net8 nuget timeonly unit-testing
Last synced: 3 days ago
JSON representation
Useful customizations for AutoFixture
- Host: GitHub
- URL: https://github.com/owen-krueger/autofixture.community.customizations
- Owner: Owen-Krueger
- License: mit
- Created: 2024-06-12T15:08:11.000Z (8 months ago)
- Default Branch: main
- Last Pushed: 2024-11-04T09:11:32.000Z (3 months ago)
- Last Synced: 2025-01-30T15:26:40.491Z (11 days ago)
- Topics: autofixture, csharp, dateonly, net6, net7, net8, nuget, timeonly, unit-testing
- Language: C#
- Homepage:
- Size: 30.3 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
- Codeowners: .github/CODEOWNERS
Awesome Lists containing this project
README
# AutoFixture Community Customizations
[![.NET](https://github.com/Owen-Krueger/AutoFixture.Community.Customizations/actions/workflows/dotnet.yaml/badge.svg)](https://github.com/Owen-Krueger/AutoFixture.Community.Customizations/actions/workflows/dotnet.yaml)
## IgnoreAutoFixture Attribute
The `IgnoreAutoFixture` attribute can be added to properties in a model to instruct AutoFixture to ignore/omit the property during building, unless explicitly instructed to be created.
This is especially useful for standing up models used by Entity Framework. Most of these models utilize circular dependencies, which would cause an exception to be raised by AutoFixture normally.
An example of this can be found below:
``` C#
public class TestModel
{
public InnerModel? IncludedModel { get; set; } // Will be instantiated by AutoFixture[IgnoreAutoFixture]
public InnerModel? OmittedModel { get; set; } // Will be omitted by AutoFixture
[IgnoreAutoFixture]
public InnerModel? ExplicitlyIncludedModel { get; set; } // Will also be omitted by AutoFixture, but will be explicitly included (see below)
}public class Tests
{
[Test]
public void AutoFixtureTests()
{
var fixture = new Fixture();
fixture.AddIgnorePropertiesCustomization();
var model = fixture.Build()
.With(x => x.ExplicitlyIncludedModel, new InnerModel()) // Tells AutoFixture to set this property, instead of ignoring it.
.Create();
Assert.That(model.IncludedModel, Is.Not.Null);
Assert.That(model.OmittedModel, Is.Null);
Assert.That(model.ExplicitlyIncludedModel, Is.Not.Null);
}
}
```## DateOnly/TimeOnly Customization
AutoFixture customizations are provided to handle `DateOnly` and `TimeOnly` properties. By default, AutoFixture doesn't handle these types and will throw exceptions.
An example of this can be found below:
``` C#
public class TestModel
{
public DateOnly? Date { get; set; }public TimeOnly? Time { get; set; }
}public class Tests
{
[Test]
public void AutoFixtureTests()
{
var fixture = new Fixture();
fixture
.AddDateOnlyCustomization()
.AddTimeOnlyCustomization();
var model = fixture.Create();
Assert.That(model.Date, Is.Not.Null);
Assert.That(model.Time, Is.Not.Null);
}
}
```## AutoFixture Extension Methods
To use any of these customizations, you can use the following extension methods:
``` C#
var fixture = new Fixture();
fixture.AddIgnorePropertiesCustomization(); // Ignore properties
fixture.AddDateOnlyCustomization(); // DateOnly
fixture.AddTimeOnlyCustomization(); // TimeOnly
```You can also chain any of these extensions together:
``` C#
var fixture = new Fixture();
fixture
.AddIgnorePropertiesCustomization()
.AddDateOnlyCustomization()
.AddTimeOnlyCustomization();
```If you'd rather, you can ignore these extension methods and add the customizations yourself manually:
``` C#
var fixture = new Fixture();
fixture.Customizations.Add(new IgnorePropertiesSpecimenBuilder()); // Ignore properties
fixture.Customize(new DateOnlyCustomization()); // DateOnly
fixture.Customize(new TimeOnlyCustomization()); // TimeOnly
```