如何对异常处理程序中间件进行单元测试

der*_*tda 5 c# unit-testing asp.net-core asp.net-core-webapi

我正在尝试使用自定义错误处理程序为我的 .NET Core 3 API 返回格式正确的异常。处理程序工作得很好,我遇到的问题是编写适当的单元测试来测试处理程序。我为此注册了中间件,如下所示:

    public void Configure(IApplicationBuilder app, IWebHostEnvironment env, IEnvService envService)
    {
        if (envService.IsNonProductionEnv())
        {
            app.UseExceptionHandler("/Error-descriptive");
        }
        else
        {
            app.UseExceptionHandler("/Error");
        }

        ...

     }
Run Code Online (Sandbox Code Playgroud)

以下是“错误描述”端点的代码:

    public IActionResult ErrorDescriptive()
    {
        if (!_env.IsNonProductionEnv())            
            throw new InvalidOperationException("This endpoint cannot be invoked in a production environment.");

        IExceptionHandlerFeature exFeature = HttpContext.Features.Get<IExceptionHandlerFeature>();

        return Problem(detail: exFeature.Error.StackTrace, title: exFeature.Error.Message);
     }
Run Code Online (Sandbox Code Playgroud)

我具体处理的问题是这一行:

IExceptionHandlerFeature exFeature = HttpContext.Features.Get<IExceptionHandlerFeature>();
Run Code Online (Sandbox Code Playgroud)

从单元测试的角度来看,我很难让它工作,因为(据我所知)这行代码从服务器获取了最新的异常。有没有一种方法可以在我的测试中设置从 HttpContext 获得的异常?另外,由于这是使用 HttpContext,我是否还需要合并它的模拟版本,例如 DefaultHttpContext?

Ron*_*ald 3

您可以通过IHttpContextAccessor在控制器中注入接口来实现这一点。该接口提供了访问 HttpContext 对象的抽象,主要是在 Web 库之外。

要使用它,您必须services.AddHttpContextAccessor();在启动文件中。然后你可以将它注入到你的控制器中。

[ApiController]
public class ErrorController
{
    private readonly ILogger<ErrorController> logger;
    private readonly IHttpContextAccessor httpContextAccessor;

    public ErrorController(ILogger<ErrorController> logger, IHttpContextAccessor httpContextAccessor){
        this.logger = logger;
        this.httpContextAccessor = httpContextAccessor ?? throw new ArgumentNullException(nameof(httpContextAccessor));
    }
}

Run Code Online (Sandbox Code Playgroud)

然后在您的方法中,您使用访问器接口中的 HttpContext 而不是基类。

[Route("/error-development")]
public IActionResult HandleErrorDevelopment() 
{
     var exceptionFeature = this.httpContextAccessor.HttpContext.Features.Get<IExceptionHandlerFeature>();
     var error = exceptionFeature.Error;
}
Run Code Online (Sandbox Code Playgroud)

由于我们现在正在针对注入的接口进行工作,因此您可以在单元测试中模拟它。请记住,您必须模拟您正在调用的整个链。(我在本例中使用 xUnit 和 Moq)。

public ErrorControllerTests() {
     this.httpContextAccessorMock = new Mock<IHttpContextAccessor>();
     this.httpContextMock = new Mock<HttpContext>();
     this.featureCollectionMock = new Mock<IFeatureCollection>();
     this.exceptionHandlerFeatureMock = new Mock<IExceptionHandlerFeature>();

     this.httpContextAccessorMock.Setup(ca => ca.HttpContext).Returns(this.httpContextMock.Object);
     this.httpContextMock.Setup(c => c.Features).Returns(this.featureCollectionMock.Object);
     this.featureCollectionMock.Setup(fc => fc.Get<IExceptionHandlerFeature>()).Returns(this.exceptionHandlerFeatureMock.Object);

     this.controller = new ErrorController(null, this.httpContextAccessorMock.Object);
}

[Fact]
public void HandleErrorDevelopment() {
    Exception thrownException;
    try{
        throw new ApplicationException("A thrown application exception");
    }
    catch(Exception ex){
        thrownException = ex;
    }
    this.exceptionHandlerFeatureMock.Setup(ehf => ehf.Error).Returns(thrownException);

    var result = this.controller.HandleErrorDevelopment();
    var objectResult = result as ObjectResult;
    Assert.NotNull(objectResult);
    Assert.Equal(500, objectResult.StatusCode);
    var problem = objectResult.Value as ProblemDetails;
    Assert.NotNull(problem);
    Assert.Equal(thrownException.Message, problem.Title);
    Assert.Equal(thrownException.StackTrace, problem.Detail);
}
Run Code Online (Sandbox Code Playgroud)

重要提示:您不能使用该ControllerBase.Problem()方法。此实现依赖于 来ControllerBase.HttpContext检索ProblemDetailsFactory对象。

你可以在源代码中看到它。由于您的单元测试没有设置上下文,因此它将抛出一个NullReferenceException.

Factory只不过是创建ProblemDetails对象的帮手。您可以再次在 GitHub 上看到默认实现

我最终创建了自己的私有方法来创建这个对象。

private ProblemDetails CreateProblemDetails(
     int? statusCode = null,
     string title = null,
     string type = null,
     string detail = null,
     string instance = null){

     statusCode ??= 500;
     var problemDetails = new ProblemDetails
     {
          Status = statusCode.Value,
          Title = title,
          Type = type,
          Detail = detail,
          Instance = instance,
     };

     var traceId = Activity.Current?.Id ?? this.httpContextAccessor.HttpContext?.TraceIdentifier;
     if (traceId != null)
     {
         problemDetails.Extensions["traceId"] = traceId;
     }

     return problemDetails;
}
Run Code Online (Sandbox Code Playgroud)

完整的错误处理方法如下所示。

[Route("/error-development")]
public IActionResult HandleErrorDevelopment() {
      var exceptionFeature = this.httpContextAccessor.HttpContext.Features.Get<IExceptionHandlerFeature>();
      var error = exceptionFeature.Error;
      this.logger?.LogError(error, "Unhandled exception");
      var problem = this.CreateProblemDetails(title: error.Message, detail: error.StackTrace);
       return new ObjectResult(problem){
           StatusCode = problem.Status
       };
}
Run Code Online (Sandbox Code Playgroud)

对于生产,我要么提供要向用户显示的通用消息,要么在已经有要显示的用户友好消息的代码中抛出自定义处理的异常。

if (error is HandledApplicationException)
{
    // handled exceptions with user-friendly message.
    problem = this.CreateProblemDetails(title: error.Message);
}
else {
    // unhandled exceptions. Provice a generic error message to display to the end user.
    problem = this.CreateProblemDetails(title: "An unexpected exception occured. Please try again or contact IT support.");
}
Run Code Online (Sandbox Code Playgroud)