Caches JsonSerializerOptions instances in tests

This commit is contained in:
BinaryAlley committed 2026-08-22 00:43:58 -07:00
1 parent c2e00242cc
commit b2b6f554b7
3 files changed
+6 -8

No files matched your search

@@ -26,6 +26,7 @@ public class GetRolesEndpointTests
private readonly IApiHttpClient _mockApiHttpClient;
private readonly GetRolesEndpoint _sut;
private readonly RoleDtoFixture _roleDtoFixture = new();
private readonly JsonSerializerOptions _jsonOptions = new() { PropertyNameCaseInsensitive = true };
/// <summary>
/// Initializes a new instance of the <see cref="GetRolesEndpointTests"/> class.
@@ -51,15 +52,10 @@ public class GetRolesEndpointTests
// Assert
using JsonDocument jsonDocument = JsonDocument.Parse(body);
Assert.True(jsonDocument.RootElement.GetProperty("success").GetBoolean());
RoleDto[]? returnedRoles = jsonDocument.RootElement.GetProperty("data").Deserialize<RoleDto[]>(CreateCaseInsensitiveOptions());
RoleDto[]? returnedRoles = jsonDocument.RootElement.GetProperty("data").Deserialize<RoleDto[]>(_jsonOptions);
Assert.Equal(expectedRoles.Select(role => role.RoleName), returnedRoles!.Select(role => role.RoleName));
}
private static JsonSerializerOptions CreateCaseInsensitiveOptions()
{
return new JsonSerializerOptions { PropertyNameCaseInsensitive = true };
}
[Fact]
public async Task ExecuteAsync_WhenCalled_ShouldRequestRolesFromApi()
{
@@ -26,6 +26,7 @@ public class GetLibrariesEndpointTests
private readonly IApiHttpClient _mockApiHttpClient;
private readonly GetLibrariesEndpoint _sut;
private readonly LibraryDtoFixture _libraryDtoFixture = new();
private readonly JsonSerializerOptions _jsonOptions = new() { PropertyNameCaseInsensitive = true };
/// <summary>
/// Initializes a new instance of the <see cref="GetLibrariesEndpointTests"/> class.
@@ -51,7 +52,7 @@ public class GetLibrariesEndpointTests
// Assert
using JsonDocument jsonDocument = JsonDocument.Parse(body);
Assert.True(jsonDocument.RootElement.GetProperty("success").GetBoolean());
LibraryDto[]? returnedLibraries = jsonDocument.RootElement.GetProperty("data").Deserialize<LibraryDto[]>(new JsonSerializerOptions { PropertyNameCaseInsensitive = true });
LibraryDto[]? returnedLibraries = jsonDocument.RootElement.GetProperty("data").Deserialize<LibraryDto[]>(_jsonOptions);
Assert.Equal(expectedLibraries.Select(library => library.Title), returnedLibraries!.Select(library => library.Title));
}
@@ -25,6 +25,7 @@ public class GetPluginsEndpointTests
{
private readonly IApiHttpClient _mockApiHttpClient;
private readonly GetPluginsEndpoint _sut;
private readonly JsonSerializerOptions _jsonOptions = new() { PropertyNameCaseInsensitive = true };
/// <summary>
/// Initializes a new instance of the <see cref="GetPluginsEndpointTests"/> class.
@@ -50,7 +51,7 @@ public class GetPluginsEndpointTests
// Assert
using JsonDocument jsonDocument = JsonDocument.Parse(body);
Assert.True(jsonDocument.RootElement.GetProperty("success").GetBoolean());
PluginDto[]? returnedPlugins = jsonDocument.RootElement.GetProperty("data").Deserialize<PluginDto[]>(new JsonSerializerOptions { PropertyNameCaseInsensitive = true });
PluginDto[]? returnedPlugins = jsonDocument.RootElement.GetProperty("data").Deserialize<PluginDto[]>(_jsonOptions);
Assert.Equal(expectedPlugins.Select(plugin => plugin.Name), returnedPlugins!.Select(plugin => plugin.Name));
}