Skip to content

Commit 4a20ef3

Browse files
authored
Merge pull request #481 from nanotaboada/test/469-validation-error-payload-assertions
test(api): add payload assertions to 422 validation error tests (#469)
2 parents da5313b + 4780e84 commit 4a20ef3

File tree

3 files changed

+25
-5
lines changed

3 files changed

+25
-5
lines changed

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ This project uses famous football stadiums (A-Z) that hosted FIFA World Cup matc
4646

4747
### Changed
4848

49-
- Field validation failures now return `422 Unprocessable Entity` (RFC 4918) instead of `400 Bad Request`; `400 Bad Request` is now reserved for malformed requests (unparseable JSON, wrong `Content-Type`, route/body mismatch) per RFC 9457
49+
- Field validation failures now return `422 Unprocessable Entity` (RFC 4918) instead of `400 Bad Request`; `400 Bad Request` is now reserved for malformed requests (unparseable JSON, route/body mismatch); unsupported media types return `415 Unsupported Media Type` (RFC 9110 §15.5.16) via the `[Consumes]` attribute. Error responses follow the Problem Details format (RFC 9457).
5050

5151
### Fixed
5252

test/Dotnet.Samples.AspNetCore.WebApi.Tests/Integration/PlayerWebApplicationTests.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -199,10 +199,10 @@ public async Task Post_Players_Nonexistent_Returns201Created()
199199

200200
// Note: the controller's 409 Conflict branch (squad number already exists) is
201201
// unreachable via the HTTP pipeline. The "Create" validation rule set includes
202-
// BeUniqueSquadNumber, which returns a 400 validation error before the
203-
// controller's own duplicate check ever runs. The 409 path is covered by the
204-
// unit test Post_Players_Existing_Returns409Conflict, where validation is mocked
205-
// to pass so the controller logic can be exercised in isolation.
202+
// BeUniqueSquadNumber, which returns a 422 validation error (Unprocessable Entity)
203+
// before the controller's own duplicate check ever runs. The 409 path is covered
204+
// by the unit test Post_Players_Existing_Returns409Conflict, where validation is
205+
// mocked to pass so the controller logic can be exercised in isolation.
206206

207207
[Fact]
208208
[Trait("Category", "Integration")]

test/Dotnet.Samples.AspNetCore.WebApi.Tests/Unit/PlayerControllerTests.cs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,16 @@ public async Task Post_Players_ValidationError_Returns422UnprocessableEntity()
6565
);
6666
var httpResult = result.Should().BeOfType<ProblemHttpResult>().Subject;
6767
httpResult.StatusCode.Should().Be(StatusCodes.Status422UnprocessableEntity);
68+
var problemDetails = httpResult
69+
.ProblemDetails.Should()
70+
.BeOfType<HttpValidationProblemDetails>()
71+
.Subject;
72+
problemDetails.Status.Should().Be(StatusCodes.Status422UnprocessableEntity);
73+
problemDetails
74+
.Errors.Should()
75+
.ContainKey("SquadNumber")
76+
.WhoseValue.Should()
77+
.Contain("SquadNumber must be greater than 0.");
6878
}
6979

7080
[Fact]
@@ -341,6 +351,16 @@ public async Task Put_PlayerBySquadNumber_ValidationError_Returns422Unprocessabl
341351
);
342352
var httpResult = result.Should().BeOfType<ProblemHttpResult>().Subject;
343353
httpResult.StatusCode.Should().Be(StatusCodes.Status422UnprocessableEntity);
354+
var problemDetails = httpResult
355+
.ProblemDetails.Should()
356+
.BeOfType<HttpValidationProblemDetails>()
357+
.Subject;
358+
problemDetails.Status.Should().Be(StatusCodes.Status422UnprocessableEntity);
359+
problemDetails
360+
.Errors.Should()
361+
.ContainKey("SquadNumber")
362+
.WhoseValue.Should()
363+
.Contain("SquadNumber must be greater than 0.");
344364
}
345365

346366
[Fact]

0 commit comments

Comments
 (0)