我試圖用DefaultHttpContext
對象來單元測試我的異常處理中間件。在單元測試情況下檢查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()。 – Kostya