如何使用 UseExceptionHandler 中间件为全局异常处理程序编写单元测试用例

Pri*_*aha 5 c# nunit exception-handling xunit.net .net-core

我创建了如下的全局异常处理程序类

public static void UseGlobalExceptionHandler(this IApplicationBuilder app, ILogger logger)
    {
        app.UseExceptionHandler(
            options => options.Run(
                async context =>
                {
                    var env = context.RequestServices.GetService<IHostingEnvironment>();
                    var general = context.RequestServices.GetService<General>();
                    var exHandlerFeature = context.Features.Get<IExceptionHandlerFeature>();
                    var exception = exHandlerFeature.Error;
                    LogException(logger, exception);
                    JObject response = CreateResponseMessage(exception,env,general);
                    if (!context.Response.HasStarted)
                    {
                        context.Response.Headers.Add("Access-Control-Allow-Credentials", new StringValues("true"));
                        context.Response.Headers.Add("Access-Control-Allow-Origin", new StringValues("*"));
                        context.Response.ContentType = "application/json";
                        context.Response.StatusCode = (int)HttpStatusCode.InternalServerError;
                        var json = JsonConvert.SerializeObject(response);
                        await context.Response.WriteAsync(json);
                    }         
                }
                )
            );
    }
Run Code Online (Sandbox Code Playgroud)

我是编写单元测试用例的新手。我想知道如何使用上述代码中提到的参数/变量来模拟异常处理中间件并为其编写测试用例。