IHttpConnectionFeature

IHttpConnectionFeature

嗨,我想在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 =“对象引用未设置为对象的实例。”

最佳答案

您可以像这样添加它:

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

关于c# - 在ASP.NET Core中模拟IHttpConnectionFeature,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/44065779/

10-11 03:34