【发布时间】:2018-02-08 02:53:13
【问题描述】:
我正在尝试使用 DefaultHttpContext 对象对我的异常处理中间件进行单元测试。
我的测试方法如下:
[Fact]
public async Task Invoke_ProductionNonSuredException_ReturnsProductionRequestError()
{
var logger = new Mock<ILogger<ExceptionHandlerMiddleware>>();
var middleWare = new ExceptionHandlerMiddleware(next: async (innerHttpContext) =>
{
await Task.Run(() =>
{
throw new Exception();
});
}, logger: logger.Object);
var mockEnv = new Mock<IHostingEnvironment>();
mockEnv.Setup(u => u.EnvironmentName).Returns("Production");
var context = new DefaultHttpContext();
await middleWare.Invoke(context, mockEnv.Object);
var reader = new StreamReader(context.Response.Body);
var streamText = reader.ReadToEnd();
//TODO: write assert that checks streamtext is the expected production return type and not the verbose development environment version.
}
在我的中间件中,我正在写这样的上下文:
public static Task WriteResponse(HttpContext context, HttpStatusCode statusCode, object responseData, Formatting jsonFormatting)
{
context.Response.ContentType = "application/json";
context.Response.StatusCode = (int)statusCode;
return context.Response.WriteAsync(JsonConvert.SerializeObject(responseData, jsonFormatting));
}
为了让您更深入地了解我采用的中间件方法,我采用了this answer here 中的方法。
当应用程序运行它是正常的管道时工作正常。但是,在测试中使用 DefaultHttpContext 方法时,响应正文总是返回为空,并且 ContentLength 为空。因此,我在测试中的streamText 变量是一个空字符串。
在这种情况下是否可以检查中间件正在写入上下文的内容?这是合适的方式,还是有更好的方式。
【问题讨论】:
-
你的 middleWare.Invoke() 发生了什么?也许你只是不在那里调用 WriteResponse()。
标签: c# unit-testing asp.net-core .net-core moq