CustomAssertions 1.4.0

dotnet add package CustomAssertions --version 1.4.0
                    
NuGet\Install-Package CustomAssertions -Version 1.4.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="CustomAssertions" Version="1.4.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="CustomAssertions" Version="1.4.0" />
                    
Directory.Packages.props
<PackageReference Include="CustomAssertions" />
                    
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 CustomAssertions --version 1.4.0
                    
#r "nuget: CustomAssertions, 1.4.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 CustomAssertions@1.4.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=CustomAssertions&version=1.4.0
                    
Install as a Cake Addin
#tool nuget:?package=CustomAssertions&version=1.4.0
                    
Install as a Cake Tool

CustomAssertions

License: MIT .NET NuGet

A modern, fluent assertion library for .NET that provides type-safe, chainable assertions with comprehensive validation capabilities. Designed to work seamlessly with xUnit, NUnit, and MSTest.

โœจ Features

  • ๐Ÿ”— Fluent API: Chain multiple assertions together for readable, expressive tests
  • ๐ŸŽฏ Type-Safe: Strong typing with compile-time validation
  • ๐Ÿ“ฆ Comprehensive: Assertions for strings, integers, collections, dates, records, structs, tuples, and more
  • ๐Ÿงช Test Framework Agnostic: Works with xUnit, NUnit, MSTest
  • ๐Ÿš€ Modern C#: Leverages latest C# features (records, pattern matching, nullable types)
  • ๐Ÿ“ Well-Documented: Full XML documentation for IntelliSense support
  • โšก Performant: Minimal overhead with efficient implementation
  • ๐Ÿ” Detailed Error Messages: Clear, descriptive failure messages

๐Ÿ“ฆ Installation

Via NuGet Package Manager

Install-Package CustomAssertions

Via .NET CLI

dotnet add package CustomAssertions

Via PackageReference

<PackageReference Include="CustomAssertions" Version="1.2.0" />

๐Ÿš€ Quick Start

using CustomAssertions.ConcreteModels;
using Xunit;

public class ExampleTests
{
    [Fact]
    public void StringAssertion_Example()
    {
        var result = new StringAssertions("Hello, World!")
            .NotNullOrEmpty()
            .HasMinimumLength(5)
            .Contains("World")
            .MatchesRegex(@"^Hello.*!")
            .Validate();

        Assert.True(result);
    }

    [Fact]
    public void IntAssertion_Example()
    {
        var result = new IntAssertions(42)
            .IsPositive()
            .IsGreaterThan(10)
            .IsLessThan(100)
            .IsInRange(1, 50)
            .Validate();

        Assert.True(result);
    }
}

๐Ÿ“š Available Assertions

Type-Specific Assertions

  • StringAssertions: null/empty checks, length validations, regex matching, contains/not contains
  • IntAssertions: positive/negative/zero, comparisons, range validation
  • LongAssertions: 64-bit integer validation, even/odd checks, range operations
  • ByteAssertions: byte-specific comparisons and range validation
  • DecimalAssertions: high-precision decimal validation, scale checks, financial operations
  • FloatingPointAssertions: approximate equality, range, special values (NaN, Infinity)
  • BoolAssertions: true/false validation, equality checks
  • CharAssertions: letter, digit, case, punctuation, whitespace checks
  • GuidAssertions: empty/not empty, version validation, format matching
  • EnumAssertions: defined values, flag operations, name validation
  • DateTimeAssertions: before/after, date range, weekend checks, component validation
  • DateTimeOffsetAssertions: timezone-aware date/time validation, offset checks
  • TimeSpanAssertions: duration validation, component checks, approximation

Collection & Complex Type Assertions

  • CollectionAssertions: empty/not empty, contains item
  • DictionaryAssertions: key/value existence, containment checks
  • TupleAssertions: length, item validation, reference equality
  • NullableAssertions: has value, conditional validation, value equality
  • RecordAssertions: value equality for records, reference checks
  • StructAssertions: default value, equality checks
  • GenericAssertions: type checks, custom predicates, equality operations
  • ExceptionAssertions: exception message, type, inner exception, stack trace validation

๏ฟฝ Aggregate Model Example: Airline Reservation System

CustomAssertions includes a complete Domain-Driven Design (DDD) aggregate model example demonstrating enterprise-level patterns and validation. This airline reservation system showcases:

๐Ÿ“ Architecture Patterns

  • Aggregate Root Pattern: Reservation manages consistency boundary
  • Entity Pattern: Flight, Passenger, Ticket with identity and lifecycle
  • Value Object Pattern: Immutable Ticket with value semantics
  • Smart Constructors: Validation at construction time
  • Encapsulation: Private setters, controlled mutation

๐Ÿ”ง Model Components

Reservation (Aggregate Root)
var flight = new Flight("AA123", "JFK", "LAX", DateTime.UtcNow.AddDays(7));
var passenger = new Passenger("John Smith", "john.smith@email.com");
var reservation = new Reservation(flight, passenger);

// Automatic ticket issuance upon reservation creation
Assert.NotNull(reservation.Ticket);
Assert.True(reservation.IsActive);

// Cancel reservation (affects ticket state)
reservation.Cancel();
Assert.False(reservation.IsActive);
Flight Entity
var flight = new Flight("AA123", "JFK", "LAX", DateTime.UtcNow.AddDays(7));

// Validation with CustomAssertions
new StringAssertions(flight.FlightNumber)
    .NotNullOrEmpty()
    .HasMinimumLength(1)
    .Validate();

new DateTimeAssertions(flight.DepartureTime)
    .IsAfter(DateTime.UtcNow)
    .Validate();
Passenger Entity
var passenger = new Passenger("John Smith", "john.smith@email.com");

// Built-in validation
Assert.Equal("John", passenger.FirstName);
Assert.Equal("Smith", passenger.LastName);

// Email validation with CustomAssertions
new StringAssertions(passenger.Email)
    .NotNullOrEmpty()
    .Contains("@")
    .Validate();
Ticket Value Object/Entity
var ticket = reservation.Ticket;

// Verify ticket properties
Assert.Equal(reservation.Id, ticket.ReservationId);
Assert.Equal(flight.FlightNumber, ticket.FlightNumber);
Assert.Equal(passenger.Name, ticket.PassengerName);

// Check ticket validity
new BoolAssertions(ticket.IsValid)
    .IsTrue()
    .Validate();

โœ… Comprehensive Test Coverage

The aggregate model includes 29 comprehensive unit tests covering:

  • โœ“ Entity Validation: Flight, Passenger, Ticket creation and validation
  • โœ“ Aggregate Consistency: Reservation maintains invariants across entities
  • โœ“ Business Rules: Cancellation workflow, state transitions
  • โœ“ Edge Cases: Null handling, empty values, boundary conditions
  • โœ“ Integration: Entity relationships and aggregate boundaries

Test Results: 581 total tests, 29 for aggregate model, 100% pass rate

See CustomAssertions.Tests/ReservationAggregateTests.cs for complete test examples.

๐Ÿ’ก Key Learnings

This aggregate demonstrates:

  1. Aggregate Boundaries: All changes flow through the aggregate root
  2. Consistency Enforcement: Ticket automatically issued upon reservation
  3. Encapsulation: Internal entities accessed only through aggregate
  4. Validation Patterns: Constructor validation + CustomAssertions for testing
  5. Immutability: Private setters prevent external mutation
  6. State Management: IsActive computed from ticket state

๏ฟฝ๐Ÿ—๏ธ Architecture

The library follows these design principles:

  • SOLID Principles: Single Responsibility, Open/Closed, Liskov Substitution, Interface Segregation, Dependency Inversion
  • Fluent Interface Pattern: Enables method chaining for readable code
  • Template Method Pattern: Base classes define structure, concrete classes implement specifics
  • Curiously Recurring Template Pattern (CRTP): Type-safe fluent chaining

๐Ÿ”ง Requirements

  • .NET 6.0 or higher (.NET 6, .NET 7, .NET 8, .NET 9 supported)
  • C# 10 or higher (for modern language features)

๐Ÿ“„ License

This project is licensed under the MIT License - see the LICENSE.txt file for details.


Made with โค๏ธ by Darel Johnson

Copyright ยฉ 2024 Darel Johnson. All rights reserved.

Product Compatible and additional computed target framework versions.
.NET net6.0 is compatible.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 is compatible.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 is compatible.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed.  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. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • net6.0

    • No dependencies.
  • net7.0

    • No dependencies.
  • net8.0

    • No dependencies.
  • net9.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.4.0 532 12/1/2025
1.3.0 523 12/1/2025
1.2.1 431 12/1/2025
1.2.0 453 11/30/2025
1.1.0 374 11/30/2025
1.0.0 381 11/30/2025

v1.4.0: Init-Only Properties - All entity classes now use modern C# init-only properties for immutability. Properties can only be set during construction, preventing accidental reassignment. Improved type safety and thread safety. 97%+ Copilot Instructions compliance. All 581 tests passing. See CHANGELOG.md for full details.