ASP.NET Core 单元测试在测试控制器问题响应时抛出 Null Exception

CNJ*_*MC1 4 c# unit-testing .net-core asp.net-core asp.net-core-webapi

我正在为我的项目创建基本的单元测试。出于某种原因,在测试我是否收到ControllerBase.Problem(String, String, Nullable<Int32>, String, String)响应时,我不断收到 NullReferenceException 。我确定问题是与控制器实际运行不符,因为当控制器运行时它似乎表现得非常好。

控制器:

        [HttpGet("{id}")]
        [Produces("application/json")]
        [ProducesResponseType(StatusCodes.Status200OK)]
        [ProducesResponseType(StatusCodes.Status400BadRequest)]
        [ProducesResponseType(StatusCodes.Status404NotFound)]
        public IActionResult GetPatient([GuidNotEmpty] Guid id)
        {
            Patient patient = null;

            patient = _patientDbService.FindPatient(id);
            if (patient == null) {
                return Problem("Patient not found.", string.Empty, StatusCodes.Status404NotFound,
                    "An error occurred.", "https://tools.ietf.org/html/rfc7231#section-6.5.1");
            }

            return Ok(patient);
        }
Run Code Online (Sandbox Code Playgroud)

测试:

        [Fact]
        public void TestGetPatientFromIdPatientNotFound()

        {
            // Act
            IActionResult result = _patientController.GetPatient(Guid.NewGuid());

            // Assert
            Assert.IsType<ObjectResult>(result);
            Assert.NotNull(((ObjectResult)result).Value);
            Assert.IsType<ProblemDetails>(((ObjectResult)result).Value);
            Assert.Equal(((ObjectResult)result).StatusCode, StatusCodes.Status404NotFound);
        }
Run Code Online (Sandbox Code Playgroud)

结果:

X PatientServiceTest.PatientServiceUnitTest.TestGetPatientFromIdPatientNotFound [1ms]
Error Message:
   System.NullReferenceException : Object reference not set to an instance of an object.
Stack Trace:
   at Microsoft.AspNetCore.Mvc.ControllerBase.Problem(String detail, String instance, Nullable`1 statusCode, String title, String type)
   at PatientService.Controllers.PatientController.GetPatient(Guid id) in /home/surafel/coding/microservices-dev/c#/PatientService/Controllers/PatientController.cs:line 43
   at PatientServiceTest.PatientServiceUnitTest.TestGetPatientFromIdPatientNotFound() in /home/surafel/coding/microservices-dev/c#/PatientServiceTest/PatientServiceUnitTest.cs:line 69
Run Code Online (Sandbox Code Playgroud)

CNJ*_*MC1 6

正如 Aluan Haddad 在评论中指出的那样,Problem()调用ProblemDetailsFactory来创建ProblemDetails由服务管理器提供的对象。服务管理器仅在应用程序运行时起作用:https://github.com/dotnet/aspnetcore/blob/master/src/Mvc/Mvc.Core/src/ControllerBase.cs#L194

ControllerBase.ProblemDetailsFactory变量可以设置,所以我创建了一个模拟ProblemDetailsFactory实例和控制器出厂设置为我的模拟的一个实例。这似乎使它起作用。

嘲笑:

    public class MockProblemDetailsFactory : ProblemDetailsFactory
    {
        public MockProblemDetailsFactory()
        {
        }

        public override ProblemDetails CreateProblemDetails(HttpContext httpContext,
            int? statusCode = default, string title = default,
            string type = default, string detail = default, string instance = default)
        {
            return new ProblemDetails() {
                Detail = detail,
                Instance = instance,
                Status = statusCode,
                Title = title,
                Type = type,
            };
        }

        public override ValidationProblemDetails CreateValidationProblemDetails(HttpContext httpContext,
            ModelStateDictionary modelStateDictionary, int? statusCode = default,
            string title = default, string type = default, string detail = default,
            string instance = default)
        {
            return new ValidationProblemDetails(new Dictionary<string, string[]>()) {
                Detail = detail,
                Instance = instance,
                Status = statusCode,
                Title = title,
                Type = type,
            };
        }
    }
Run Code Online (Sandbox Code Playgroud)

我在此单元测试的设置中添加了这一行,它解决了问题。

_patientController.ProblemDetailsFactory = new MockProblemDetailsFactory();
Run Code Online (Sandbox Code Playgroud)