LexAssert 3.0.0

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

LexAssert

LexAssert extends Assert

Motivaton

A common difficulty with unit test writing is asserting equality between an object generated by code under test and an object with expected set of values.

LexAssert provides ways of expressing that objects are "equal enough" for the purposes of an xUnit test.

v3.0.0 Breaking Changes

  • Target framework updated to .NET 8.0, dropping support for older .NET versions.
  • xUnit v3 packages used, replacing xUnit v2 dependencies.

Public objects

Lassert exposes some new asserts for use with xUnit. JsonEqualityComparer can be used to compare Json serializations of an object

Public methods of Lassert

void JsonEqual<T>(T expected, T actual) passes if expected and actual yield identical strings when serialized in Json. Throws an EqualException if the serialized strings are not equal.

Examples:

internal class MyTestClass
{
    string PropA => "foo";
    int PropB => 63;
}

[Fact]
public void JsonEqual_Demo1()
{
    var actual = new MyTestClass();
    var expected = new MyTestClass();

    Lassert.JsonEqual(expected, actual); // Passes
}

[Fact]
public void JsonEqual_Demo2()
{
	var expected = new Dictionary<string, object>
	{
		{ "PropA", "foo" },
		{ "PropB", 63 }
	};

	var actual = new MyTestClass();

	Lassert.JsonEqual(expected, actual); // Passes, as both objects yield identical Json.
}

public static void MembersEqual<T>(T expected, T actual, params Func<T, object>[] memberSelectors) passes if the selector functions, when applied to each of the inputs, yield equal values in each case. Throws an EqualException if the values are different.

This can be used to specify which members of a type are important when comparing.

Example:

internal class AnotherTestClass
{
    public string PropA { get; set; }
    public int PropB { get; set; }
    public System.Guid PropC { get; set; }
}

[Fact]
public void MembersEqual_Demo1()
{
    var actual = new AnotherTestClass
    {
        PropA = "foo",
        PropB = 63,
        PropC = System.Guid.NewGuid()
    };

    var expected = new AnotherTestClass
    {
        PropA = "foo",
        PropB = 63,
        PropC = System.Guid.NewGuid()
    };

    Lassert.MembersEqual(expected, actual,
        c => c.PropA,
        c => c.PropB);  // Passes, because PropC is not compared.
}

The functions comparing the two objects can be more complicated if needed:

[Fact]
public void MembersEqual_Demo2()
{
    var actual = "foo";
    var expected = "bar";

    Lassert.MembersEqual(expected, actual,
        c => c.ToLowerInvariant().Contains("z")); // Passes, because false == false.
}
Product Compatible and additional computed target framework versions.
.NET 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 was computed.  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.

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
3.0.0 0 2/16/2026
2.2.1 767 12/13/2024
2.1.0 278 7/2/2024
2.0.0 1,688 1/10/2023
1.0.1 840 7/17/2022
1.0.0 568 7/17/2022

BREAKING CHANGE: Upgraded to .NET 8.0 and xUnit v3. Minimum requirements: .NET 8+ or .NET Framework 4.7.2+. Public API unchanged. See README for migration guide.