Skip to content

Commit ed0c42e

Browse files
committed
Simplify meta:total assertions and produce better error on failure
Before: Expected element.GetInt32() to be 25, but found 2 (difference of -23). After: Expected responseDocument.Meta to be 25, but found 2 (difference of -23).
1 parent cb4c793 commit ed0c42e

File tree

4 files changed

+18
-43
lines changed

4 files changed

+18
-43
lines changed

test/JsonApiDotNetCoreTests/IntegrationTests/Meta/TopLevelCountTests.cs

+3-16
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
using System.Net;
2-
using System.Text.Json;
32
using FluentAssertions;
43
using JsonApiDotNetCore.Configuration;
54
using JsonApiDotNetCore.Resources;
@@ -54,11 +53,7 @@ await _testContext.RunOnDatabaseAsync(async dbContext =>
5453
// Assert
5554
httpResponse.ShouldHaveStatusCode(HttpStatusCode.OK);
5655

57-
responseDocument.Meta.ShouldContainKey("total").With(value =>
58-
{
59-
JsonElement element = value.Should().BeOfType<JsonElement>().Subject;
60-
element.GetInt32().Should().Be(1);
61-
});
56+
responseDocument.Meta.Should().ContainTotal(1);
6257
}
6358

6459
[Fact]
@@ -84,11 +79,7 @@ await _testContext.RunOnDatabaseAsync(async dbContext =>
8479
// Assert
8580
httpResponse.ShouldHaveStatusCode(HttpStatusCode.OK);
8681

87-
responseDocument.Meta.ShouldContainKey("total").With(value =>
88-
{
89-
JsonElement element = value.Should().BeOfType<JsonElement>().Subject;
90-
element.GetInt32().Should().Be(1);
91-
});
82+
responseDocument.Meta.Should().ContainTotal(1);
9283
}
9384

9485
[Fact]
@@ -108,11 +99,7 @@ await _testContext.RunOnDatabaseAsync(async dbContext =>
10899
// Assert
109100
httpResponse.ShouldHaveStatusCode(HttpStatusCode.OK);
110101

111-
responseDocument.Meta.ShouldContainKey("total").With(value =>
112-
{
113-
JsonElement element = value.Should().BeOfType<JsonElement>().Subject;
114-
element.GetInt32().Should().Be(0);
115-
});
102+
responseDocument.Meta.Should().ContainTotal(0);
116103
}
117104

118105
[Fact]

test/JsonApiDotNetCoreTests/IntegrationTests/NamingConventions/KebabCasingTests.cs

+1-6
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
using System.Net;
2-
using System.Text.Json;
32
using FluentAssertions;
43
using JsonApiDotNetCore.Serialization.Objects;
54
using TestBuildingBlocks;
@@ -57,11 +56,7 @@ await _testContext.RunOnDatabaseAsync(async dbContext =>
5756
responseDocument.Included[0].Relationships.Should().BeNull();
5857
responseDocument.Included[0].Links.ShouldNotBeNull().Self.Should().Be($"/public-api/diving-boards/{pools[1].DivingBoards[0].StringId}");
5958

60-
responseDocument.Meta.ShouldContainKey("total").With(value =>
61-
{
62-
JsonElement element = value.Should().BeOfType<JsonElement>().Subject;
63-
element.GetInt32().Should().Be(2);
64-
});
59+
responseDocument.Meta.Should().ContainTotal(2);
6560
}
6661

6762
[Fact]

test/JsonApiDotNetCoreTests/IntegrationTests/ResourceDefinitions/Reading/ResourceDefinitionReadTests.cs

+4-21
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
using System.Net;
2-
using System.Text.Json;
32
using FluentAssertions;
43
using JsonApiDotNetCore.Configuration;
54
using JsonApiDotNetCore.Serialization.Objects;
@@ -238,11 +237,7 @@ await _testContext.RunOnDatabaseAsync(async dbContext =>
238237
responseDocument.Data.ManyValue[0].Id.Should().Be(planets[1].StringId);
239238
responseDocument.Data.ManyValue[1].Id.Should().Be(planets[3].StringId);
240239

241-
responseDocument.Meta.ShouldContainKey("total").With(value =>
242-
{
243-
JsonElement element = value.Should().BeOfType<JsonElement>().Subject;
244-
element.GetInt32().Should().Be(2);
245-
});
240+
responseDocument.Meta.Should().ContainTotal(2);
246241

247242
hitCounter.HitExtensibilityPoints.Should().BeEquivalentTo(new[]
248243
{
@@ -297,11 +292,7 @@ await _testContext.RunOnDatabaseAsync(async dbContext =>
297292
responseDocument.Data.ManyValue.ShouldHaveCount(1);
298293
responseDocument.Data.ManyValue[0].Id.Should().Be(planets[3].StringId);
299294

300-
responseDocument.Meta.ShouldContainKey("total").With(value =>
301-
{
302-
JsonElement element = value.Should().BeOfType<JsonElement>().Subject;
303-
element.GetInt32().Should().Be(1);
304-
});
295+
responseDocument.Meta.Should().ContainTotal(1);
305296

306297
hitCounter.HitExtensibilityPoints.Should().BeEquivalentTo(new[]
307298
{
@@ -349,11 +340,7 @@ await _testContext.RunOnDatabaseAsync(async dbContext =>
349340
responseDocument.Data.ManyValue[0].Id.Should().Be(star.Planets.ElementAt(1).StringId);
350341
responseDocument.Data.ManyValue[1].Id.Should().Be(star.Planets.ElementAt(3).StringId);
351342

352-
responseDocument.Meta.ShouldContainKey("total").With(value =>
353-
{
354-
JsonElement element = value.Should().BeOfType<JsonElement>().Subject;
355-
element.GetInt32().Should().Be(2);
356-
});
343+
responseDocument.Meta.Should().ContainTotal(2);
357344

358345
hitCounter.HitExtensibilityPoints.Should().BeEquivalentTo(new[]
359346
{
@@ -405,11 +392,7 @@ await _testContext.RunOnDatabaseAsync(async dbContext =>
405392
responseDocument.Data.ManyValue[0].Id.Should().Be(star.Planets.ElementAt(1).StringId);
406393
responseDocument.Data.ManyValue[1].Id.Should().Be(star.Planets.ElementAt(3).StringId);
407394

408-
responseDocument.Meta.ShouldContainKey("total").With(value =>
409-
{
410-
JsonElement element = value.Should().BeOfType<JsonElement>().Subject;
411-
element.GetInt32().Should().Be(2);
412-
});
395+
responseDocument.Meta.Should().ContainTotal(2);
413396

414397
hitCounter.HitExtensibilityPoints.Should().BeEquivalentTo(new[]
415398
{

test/TestBuildingBlocks/ObjectAssertionsExtensions.cs

+10
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using System.Text.Encodings.Web;
33
using System.Text.Json;
44
using FluentAssertions;
5+
using FluentAssertions.Collections;
56
using FluentAssertions.Numeric;
67
using FluentAssertions.Primitives;
78
using JetBrains.Annotations;
@@ -65,4 +66,13 @@ private static string ToJsonString(JsonDocument document)
6566
writer.Flush();
6667
return Encoding.UTF8.GetString(stream.ToArray());
6768
}
69+
70+
/// <summary>
71+
/// Asserts that a "meta" dictionary contains a single element named "total" with the specified value.
72+
/// </summary>
73+
[CustomAssertion]
74+
public static void ContainTotal(this GenericDictionaryAssertions<IDictionary<string, object?>, string, object?> source, int expectedTotal)
75+
{
76+
source.ContainKey("total").WhoseValue.Should().BeOfType<JsonElement>().Subject.GetInt32().Should().Be(expectedTotal);
77+
}
6878
}

0 commit comments

Comments
 (0)