Allow ProblemDetails \ ValidationProblemDetails to be more testable
Fixes https://github.com/dotnet/aspnetcore/issues/15166
This commit is contained in:
parent
94e314d4d2
commit
2d6fd453e2
|
|
@ -1886,13 +1886,29 @@ namespace Microsoft.AspNetCore.Mvc
|
||||||
string title = null,
|
string title = null,
|
||||||
string type = null)
|
string type = null)
|
||||||
{
|
{
|
||||||
var problemDetails = ProblemDetailsFactory.CreateProblemDetails(
|
ProblemDetails problemDetails;
|
||||||
HttpContext,
|
if (ProblemDetailsFactory == null)
|
||||||
statusCode: statusCode ?? 500,
|
{
|
||||||
title: title,
|
// ProblemDetailsFactory may be null in unit testing scenarios. Improvise to make this more testable.
|
||||||
type: type,
|
problemDetails = new ProblemDetails
|
||||||
detail: detail,
|
{
|
||||||
instance: instance);
|
Detail = detail,
|
||||||
|
Instance = instance,
|
||||||
|
Status = statusCode ?? 500,
|
||||||
|
Title = title,
|
||||||
|
Type = type,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
problemDetails = ProblemDetailsFactory.CreateProblemDetails(
|
||||||
|
HttpContext,
|
||||||
|
statusCode: statusCode ?? 500,
|
||||||
|
title: title,
|
||||||
|
type: type,
|
||||||
|
detail: detail,
|
||||||
|
instance: instance);
|
||||||
|
}
|
||||||
|
|
||||||
return new ObjectResult(problemDetails)
|
return new ObjectResult(problemDetails)
|
||||||
{
|
{
|
||||||
|
|
@ -1958,14 +1974,30 @@ namespace Microsoft.AspNetCore.Mvc
|
||||||
{
|
{
|
||||||
modelStateDictionary ??= ModelState;
|
modelStateDictionary ??= ModelState;
|
||||||
|
|
||||||
var validationProblem = ProblemDetailsFactory.CreateValidationProblemDetails(
|
ValidationProblemDetails validationProblem;
|
||||||
HttpContext,
|
if (ProblemDetailsFactory == null)
|
||||||
modelStateDictionary,
|
{
|
||||||
statusCode: statusCode,
|
// ProblemDetailsFactory may be null in unit testing scenarios. Improvise to make this more testable.
|
||||||
title: title,
|
validationProblem = new ValidationProblemDetails(modelStateDictionary)
|
||||||
type: type,
|
{
|
||||||
detail: detail,
|
Detail = detail,
|
||||||
instance: instance);
|
Instance = instance,
|
||||||
|
Status = statusCode,
|
||||||
|
Title = title,
|
||||||
|
Type = type,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
validationProblem = ProblemDetailsFactory?.CreateValidationProblemDetails(
|
||||||
|
HttpContext,
|
||||||
|
modelStateDictionary,
|
||||||
|
statusCode: statusCode,
|
||||||
|
title: title,
|
||||||
|
type: type,
|
||||||
|
detail: detail,
|
||||||
|
instance: instance);
|
||||||
|
}
|
||||||
|
|
||||||
if (validationProblem.Status == 400)
|
if (validationProblem.Status == 400)
|
||||||
{
|
{
|
||||||
|
|
|
||||||
|
|
@ -373,7 +373,7 @@ namespace Microsoft.AspNetCore.Mvc
|
||||||
Assert.Equal(routeName, acceptedAtRouteResult.RouteName);
|
Assert.Equal(routeName, acceptedAtRouteResult.RouteName);
|
||||||
Assert.Single(acceptedAtRouteResult.RouteValues);
|
Assert.Single(acceptedAtRouteResult.RouteValues);
|
||||||
Assert.Equal("sample", acceptedAtRouteResult.RouteValues["route"]);
|
Assert.Equal("sample", acceptedAtRouteResult.RouteValues["route"]);
|
||||||
Assert.Same(value,acceptedAtRouteResult.Value);
|
Assert.Same(value, acceptedAtRouteResult.Value);
|
||||||
|
|
||||||
// Arrange
|
// Arrange
|
||||||
controller = new TestabilityController();
|
controller = new TestabilityController();
|
||||||
|
|
@ -682,6 +682,42 @@ namespace Microsoft.AspNetCore.Mvc
|
||||||
Assert.Equal(new { Arg1 = "Hi", Arg2 = "There" }, result.Arguments);
|
Assert.Equal(new { Arg1 = "Hi", Arg2 = "There" }, result.Arguments);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void Problem_Works()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
var detail = "Some random error";
|
||||||
|
var controller = new TestabilityController();
|
||||||
|
|
||||||
|
// Act
|
||||||
|
var result = controller.Problem(detail);
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
var badRequest = Assert.IsType<ObjectResult>(result);
|
||||||
|
var problemDetails = Assert.IsType<ProblemDetails>(badRequest.Value);
|
||||||
|
Assert.Equal(detail, problemDetails.Detail);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void ValidationProblem_Works()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
var detail = "Some random error";
|
||||||
|
var controller = new TestabilityController();
|
||||||
|
|
||||||
|
// Act
|
||||||
|
controller.ModelState.AddModelError("some-key", "some-error");
|
||||||
|
var result = controller.ValidationProblem(detail);
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
var badRequest = Assert.IsType<ObjectResult>(result);
|
||||||
|
var validationProblemDetails = Assert.IsType<ValidationProblemDetails>(badRequest.Value);
|
||||||
|
Assert.Equal(detail, validationProblemDetails.Detail);
|
||||||
|
var error = Assert.Single(validationProblemDetails.Errors);
|
||||||
|
Assert.Equal("some-key", error.Key);
|
||||||
|
Assert.Equal(new[] { "some-error" }, error.Value);
|
||||||
|
}
|
||||||
|
|
||||||
public static IEnumerable<object[]> TestabilityViewTestData
|
public static IEnumerable<object[]> TestabilityViewTestData
|
||||||
{
|
{
|
||||||
get
|
get
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue