相关疑难解决方法(0)

在单元测试情况下检查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.
} …
Run Code Online (Sandbox Code Playgroud)

c# unit-testing moq .net-core asp.net-core

8
推荐指数
1
解决办法
2062
查看次数

标签 统计

.net-core ×1

asp.net-core ×1

c# ×1

moq ×1

unit-testing ×1