PosInformatique.FluentAssertions.Json
1.0.4
Prefix Reserved
See the version list below for details.
dotnet add package PosInformatique.FluentAssertions.Json --version 1.0.4
NuGet\Install-Package PosInformatique.FluentAssertions.Json -Version 1.0.4
<PackageReference Include="PosInformatique.FluentAssertions.Json" Version="1.0.4" />
paket add PosInformatique.FluentAssertions.Json --version 1.0.4
#r "nuget: PosInformatique.FluentAssertions.Json, 1.0.4"
// Install PosInformatique.FluentAssertions.Json as a Cake Addin #addin nuget:?package=PosInformatique.FluentAssertions.Json&version=1.0.4 // Install PosInformatique.FluentAssertions.Json as a Cake Tool #tool nuget:?package=PosInformatique.FluentAssertions.Json&version=1.0.4
PosInformatique.FluentAssertions.Json
PosInformatique.FluentAssertions.Json is a library to assert JSON serialization using the Fluent Assertions library style coding.
Installing from NuGet
The PosInformatique.FluentAssertions.Json library is available directly on the official website.
To download and install the library to your Visual Studio unit test projects use the following NuGet command line
Install-Package PosInformatique.FluentAssertions.Json
How it is work?
Imagine that you have the following JSON class:
public class Customer
{
[JsonPropertyName("id")]
[JsonPropertyOrder(1)]
public int Id { get; set; }
[JsonPropertyName("name")]
[JsonPropertyOrder(2)]
public string Name { get; set; }
}
With the following instance:
var customer = new Customer()
{
Id = 1234,
Name = "Gilles TOURREAU",
};
You would like to check this class is serializable into the following JSON object:
{
"id": 1234,
"name": "Gilles TOURREAU",
}
Using standard assertions you should write the following code 😨:
[Fact]
public void Serialization()
{
var customer = new Customer()
{
Id = 1234,
Name = "Gilles TOURREAU",
};
var json = JsonSerializer.Serialize(customer);
json.Should().Be("{\"id\":1234,\"name\":\"Gilles TOURREAU\"}");
// Or
json.Should().Be(@"{""id"":1234,""name"":""Gilles TOURREAU""}");
}
With the following kind of exception when the unit test is incorrect:
As you can see the previous code is not sexy to read (and to write!) and the exception is hard to understand...
Test the serialization of a .NET Object to a JSON object.
With the new fluent style using this library you can write previous unit test like that:
[Fact]
public void Serialization()
{
var customer = new Customer()
{
Id = 1234,
Name = "Gilles TOURREAU",
};
customer.Should().BeJsonSerializableInto(new
{
id = 1234,
name = "Gilles TOURREAU",
});
}
And when an exception is occured, the exception message contains the JSON path of the property which is error:
Test the deserialization of a JSON object to a .NET Object
You can in the same way test the deserialization JSON object into a .NET object.
[Fact]
public void Deserialization()
{
var json = new
{
id = 1234,
name = "Gilles TOURREAU",
};
json.Should().BeJsonDeserializableInto(new Customer()
{
Id = 1234,
Name = "Gilles TOURREAU",
});
}
Library dependencies
The PosInformatique.FluentAssertions.Json library target the .NET Standard 2.0 and can be used with various of .NET architecture (.NET Core, .NET Framework,...).
The PosInformatique.FluentAssertions.Json library use the 5.0.0 version of the System.Text.Json NuGet package and can be used with old projects that target this library version and earlier.
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
- FluentAssertions (>= 5.0.0)
- System.Text.Json (>= 5.0.0)
NuGet packages (1)
Showing the top 1 NuGet packages that depend on PosInformatique.FluentAssertions.Json:
Package | Downloads |
---|---|
PosInformatique.Testing.Azure.Functions.Http
This project provides utilities for testing Azure Functions (Isolated). It includes features and tools to facilitate the testing of HttpRequestData and HttpResponseData classes, which are challenging to mock. |
GitHub repositories
This package is not used by any popular GitHub repositories.
1.0.4
- Ignore the properties with the [JsonIgnore] attribute.
1.0.3
- Target the .NET Standard 2.0 instead of .NET Core 6.0
1.0.2
- Fix the README.md file.
1.0.1
- Various fixes for the NuGet package description.
1.0.0
- Initial version