gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 12:37:05 29 4
gpt4 key购买 nike

我正在尝试使用自定义错误处理程序为我的 .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?

最佳答案

您可以通过注入(inject) IHttpContextAccessor 来实现这一点。 Controller 中的界面。
这个接口(interface)提供了一个抽象来访问 HttpContext 对象,主要是在你的 web 库之外。

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

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


然后在您的方法中,您使用访问器接口(interface)中的 HttpContext 而不是基类。
[Route("/error-development")]
public IActionResult HandleErrorDevelopment()
{
var exceptionFeature = this.httpContextAccessor.HttpContext.Features.Get<IExceptionHandlerFeature>();
var error = exceptionFeature.Error;
}

由于我们现在针对注入(inject)的接口(interface)工作,因此您可以在单元测试中模拟它。请记住,您必须模拟您正在调用的整个链。 (我在这个例子中使用 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 .

工厂无非是创建 ProblemDetails 的 helper 目的。您再次可以看到 Default implementation on GitHub .

我最终创建了自己的私有(private)方法来创建这个对象。
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.");
}

关于c# - 如何对异常处理程序中间件进行单元测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60288329/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com