Fluent Assertion aspect-oriented library for .NET Standard 2.0+
Goodtocode.Assertion is a .NET library providing fluent assertion and validation utilities for C# projects. It enables developers to write expressive, readable, and maintainable assertions for business logic, unit tests, and runtime validation. The library is designed for extensibility and can be integrated into any .NET Standard 2.0+ project.
- Fluent assertion syntax for clear, readable code
- Customizable assertion rules and exception handling
- Assertion scopes for grouping related checks
- Lightweight, dependency-free, and compatible with .NET Standard 2.0+
- Designed for use in both production code and unit tests
- Clone this repository
git clone https://github.com/goodtocode/aspect-assertion.git - Install .NET SDK (latest recommended)
winget install Microsoft.DotNet.SDK --silent - Build the solution
cd src dotnet build Goodtocode.Assertion.sln - Run tests
cd Goodtocode.Assertion.Tests dotnet test
- .NET SDK (latest)
- Visual Studio (latest) or VS Code
using Goodtocode.Assertion;
int value = 5;
AssertionScope.Begin()
.Assert(() => value > 0, "Value must be positive.")
.Assert(() => value < 10, "Value must be less than 10.")
.End();using Goodtocode.Assertion;
var user = new User { Name = "Alice", Age = 30 };
AssertionScope.Begin()
.Assert(() => !string.IsNullOrWhiteSpace(user.Name), "Name is required.")
.Assert(() => user.Age >= 18, "User must be an adult.")
.End();using Goodtocode.Assertion;
public class GetMyUsersPaginatedQueryValidator : Validator<GetMyUsersPaginatedQuery>
{
public GetMyUsersPaginatedQueryValidator()
{
RuleFor(v => v.StartDate).NotEmpty()
.When(v => v.EndDate != null)
.LessThanOrEqualTo(v => v.EndDate);
RuleFor(v => v.EndDate)
.NotEmpty()
.When(v => v.StartDate != null)
.GreaterThanOrEqualTo(v => v.StartDate);
RuleFor(x => x.PageNumber).NotEqual(0);
RuleFor(x => x.PageSize).NotEqual(0);
}
}using Goodtocode.Assertion;
using Microsoft.VisualStudio.TestTools.UnitTesting;
[TestClass]
public class AssertionTests
{
[TestMethod]
public void Should_Throw_When_Assertion_Fails()
{
Assert.ThrowsException<AssertionFailedException>(() =>
AssertionScope.Begin()
.Assert(() => false, "This should fail.")
.End());
}
}| Version | Date | Release Notes |
|---|---|---|
| 1.1.28 | 2026-Jan-20 | NuGet (semver) instead of file version. |
| 1.0.0 | 2026-Jan-19 | Initial release |
This project is licensed with the MIT license.