【问题标题】:How to Unit Test Exception Handler Middleware如何对异常处理程序中间件进行单元测试
【发布时间】:2020-06-02 21:54:46
【问题描述】:

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

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

        ...

     }

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

    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);
     }

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

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

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

【问题讨论】:

    标签: c# unit-testing asp.net-core asp.net-core-webapi


    【解决方案1】:

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

    要使用它,你必须在你的启动文件中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));
        }
    }
    
    

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

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

    由于我们现在正在处理注入接口,因此您可以在单元测试中对其进行模拟。请记住,您必须模拟您正在调用的整个链条。 (我在这个例子中使用 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);
    }
    

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

    您可以在source code 中看到它。由于您的单元测试没有设置上下文,它会抛出一个NullReferenceException

    Factory 只不过是创建ProblemDetails 对象的助手。你可以再次看到Default implementation on 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;
    }
    

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

    [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
           };
    }
    

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

    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.");
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-07-01
      • 1970-01-01
      相关资源
      最近更新 更多