在ASP.NET Core中模拟IHttpConnectionFeature

时间:2017-05-19 09:04:00

标签: c# asp.net unit-testing asp.net-core httpcontext

您好我想在ASP.NET Core中模拟IHttpConnectionFeature

在我的控制器中我有:

var connectionId = HttpContext.Features.Get<IHttpConnectionFeature>().ConnectionId;

但我如何在单元测试中模拟它:

        var controller = new MyController(logger.Object,
            mockService.Object)
        {
            ControllerContext = new ControllerContext
            {
                HttpContext = new DefaultHttpContext()
            }
        };

我收到错误消息:

  

Message =&#34;对象引用未设置为对象的实例。&#34;

1 个答案:

答案 0 :(得分:1)

您可以像以下一样添加:

controller.HttpContext.Features.Set<IHttpConnectionFeature>(new HttpConnectionFeature()
{
     ConnectionId = "test"
});