LexAssert 2.0.0
dotnet add package LexAssert --version 2.0.0
NuGet\Install-Package LexAssert -Version 2.0.0
<PackageReference Include="LexAssert" Version="2.0.0" />
paket add LexAssert --version 2.0.0
#r "nuget: LexAssert, 2.0.0"
// Install LexAssert as a Cake Addin
#addin nuget:?package=LexAssert&version=2.0.0
// Install LexAssert as a Cake Tool
#tool nuget:?package=LexAssert&version=2.0.0
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.
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 | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net5.0 was computed. net5.0-windows was computed. net6.0 was computed. 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 was computed. 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 was computed. 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. |
.NET Core | netcoreapp2.0 was computed. netcoreapp2.1 was computed. netcoreapp2.2 was computed. netcoreapp3.0 was computed. netcoreapp3.1 was computed. |
.NET Standard | netstandard2.0 is compatible. netstandard2.1 was computed. |
.NET Framework | net461 was computed. net462 was computed. net463 was computed. net47 was computed. net471 was computed. net472 was computed. net48 was computed. net481 was computed. |
MonoAndroid | monoandroid was computed. |
MonoMac | monomac was computed. |
MonoTouch | monotouch was computed. |
Tizen | tizen40 was computed. tizen60 was computed. |
Xamarin.iOS | xamarinios was computed. |
Xamarin.Mac | xamarinmac was computed. |
Xamarin.TVOS | xamarintvos was computed. |
Xamarin.WatchOS | xamarinwatchos was computed. |
-
.NETStandard 2.0
- JsonDiffPatch.Net (>= 2.3.0)
- System.Text.Json (>= 7.0.1)
- xunit (>= 2.4.1)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
Major update: Switch engine from NewtonSoft to System.Text.Json