2017-05-19 42 views
0

您好我想嘲笑IHttpConnectionFeature在ASP.NET核心模拟IHttpConnectionFeature在ASP.NET核心

在我的控制,我有:

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

,但我怎么能嘲笑它在我的单元测试:

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

我收到一条错误消息:

Message =“对象引用未设置为对象的实例。”

回答

1

您可以将其添加喜欢:

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