Skip to content

Commit 2d6fd45

Browse files
committed
Allow ProblemDetails \ ValidationProblemDetails to be more testable
Fixes #15166
1 parent 94e314d commit 2d6fd45

File tree

2 files changed

+84
-16
lines changed

2 files changed

+84
-16
lines changed

src/Mvc/Mvc.Core/src/ControllerBase.cs

Lines changed: 47 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1886,13 +1886,29 @@ public virtual ObjectResult Problem(
18861886
string title = null,
18871887
string type = null)
18881888
{
1889-
var problemDetails = ProblemDetailsFactory.CreateProblemDetails(
1890-
HttpContext,
1891-
statusCode: statusCode ?? 500,
1892-
title: title,
1893-
type: type,
1894-
detail: detail,
1895-
instance: instance);
1889+
ProblemDetails problemDetails;
1890+
if (ProblemDetailsFactory == null)
1891+
{
1892+
// ProblemDetailsFactory may be null in unit testing scenarios. Improvise to make this more testable.
1893+
problemDetails = new ProblemDetails
1894+
{
1895+
Detail = detail,
1896+
Instance = instance,
1897+
Status = statusCode ?? 500,
1898+
Title = title,
1899+
Type = type,
1900+
};
1901+
}
1902+
else
1903+
{
1904+
problemDetails = ProblemDetailsFactory.CreateProblemDetails(
1905+
HttpContext,
1906+
statusCode: statusCode ?? 500,
1907+
title: title,
1908+
type: type,
1909+
detail: detail,
1910+
instance: instance);
1911+
}
18961912

18971913
return new ObjectResult(problemDetails)
18981914
{
@@ -1958,14 +1974,30 @@ public virtual ActionResult ValidationProblem(
19581974
{
19591975
modelStateDictionary ??= ModelState;
19601976

1961-
var validationProblem = ProblemDetailsFactory.CreateValidationProblemDetails(
1962-
HttpContext,
1963-
modelStateDictionary,
1964-
statusCode: statusCode,
1965-
title: title,
1966-
type: type,
1967-
detail: detail,
1968-
instance: instance);
1977+
ValidationProblemDetails validationProblem;
1978+
if (ProblemDetailsFactory == null)
1979+
{
1980+
// ProblemDetailsFactory may be null in unit testing scenarios. Improvise to make this more testable.
1981+
validationProblem = new ValidationProblemDetails(modelStateDictionary)
1982+
{
1983+
Detail = detail,
1984+
Instance = instance,
1985+
Status = statusCode,
1986+
Title = title,
1987+
Type = type,
1988+
};
1989+
}
1990+
else
1991+
{
1992+
validationProblem = ProblemDetailsFactory?.CreateValidationProblemDetails(
1993+
HttpContext,
1994+
modelStateDictionary,
1995+
statusCode: statusCode,
1996+
title: title,
1997+
type: type,
1998+
detail: detail,
1999+
instance: instance);
2000+
}
19692001

19702002
if (validationProblem.Status == 400)
19712003
{

src/Mvc/Mvc.ViewFeatures/test/ControllerUnitTestabilityTests.cs

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -373,7 +373,7 @@ public void ControllerAcceptedAtRoute_InvokedInUnitTests()
373373
Assert.Equal(routeName, acceptedAtRouteResult.RouteName);
374374
Assert.Single(acceptedAtRouteResult.RouteValues);
375375
Assert.Equal("sample", acceptedAtRouteResult.RouteValues["route"]);
376-
Assert.Same(value,acceptedAtRouteResult.Value);
376+
Assert.Same(value, acceptedAtRouteResult.Value);
377377

378378
// Arrange
379379
controller = new TestabilityController();
@@ -682,6 +682,42 @@ public void ViewComponent_WithArguments()
682682
Assert.Equal(new { Arg1 = "Hi", Arg2 = "There" }, result.Arguments);
683683
}
684684

685+
[Fact]
686+
public void Problem_Works()
687+
{
688+
// Arrange
689+
var detail = "Some random error";
690+
var controller = new TestabilityController();
691+
692+
// Act
693+
var result = controller.Problem(detail);
694+
695+
// Assert
696+
var badRequest = Assert.IsType<ObjectResult>(result);
697+
var problemDetails = Assert.IsType<ProblemDetails>(badRequest.Value);
698+
Assert.Equal(detail, problemDetails.Detail);
699+
}
700+
701+
[Fact]
702+
public void ValidationProblem_Works()
703+
{
704+
// Arrange
705+
var detail = "Some random error";
706+
var controller = new TestabilityController();
707+
708+
// Act
709+
controller.ModelState.AddModelError("some-key", "some-error");
710+
var result = controller.ValidationProblem(detail);
711+
712+
// Assert
713+
var badRequest = Assert.IsType<ObjectResult>(result);
714+
var validationProblemDetails = Assert.IsType<ValidationProblemDetails>(badRequest.Value);
715+
Assert.Equal(detail, validationProblemDetails.Detail);
716+
var error = Assert.Single(validationProblemDetails.Errors);
717+
Assert.Equal("some-key", error.Key);
718+
Assert.Equal(new[] { "some-error" }, error.Value);
719+
}
720+
685721
public static IEnumerable<object[]> TestabilityViewTestData
686722
{
687723
get

0 commit comments

Comments
 (0)