PosInformatique.FluentAssertions.Json
1.0.2
Prefix Reserved
See the version list below for details.
dotnet add package PosInformatique.FluentAssertions.Json --version 1.0.2
NuGet\Install-Package PosInformatique.FluentAssertions.Json -Version 1.0.2
<PackageReference Include="PosInformatique.FluentAssertions.Json" Version="1.0.2" />
paket add PosInformatique.FluentAssertions.Json --version 1.0.2
#r "nuget: PosInformatique.FluentAssertions.Json, 1.0.2"
// Install PosInformatique.FluentAssertions.Json as a Cake Addin #addin nuget:?package=PosInformatique.FluentAssertions.Json&version=1.0.2 // Install PosInformatique.FluentAssertions.Json as a Cake Tool #tool nuget:?package=PosInformatique.FluentAssertions.Json&version=1.0.2
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",
});
}
Product | Versions 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 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. |
-
net6.0
- FluentAssertions (>= 6.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.2
- Fix the README.md file.
1.0.1
- Various fixes for the NuGet package description.
1.0.0
- Initial version