简体   繁体   中英

Mock IHttpConnectionFeature in ASP.NET Core

Hi I would like to mock IHttpConnectionFeature in ASP.NET Core

In my controller I have:

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

but how can I mock it in my unit test:

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

I'm getting an error message:

Message = "Object reference not set to an instance of an object."

You can add it like:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM