mskuratowski
mskuratowski

Reputation: 4124

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."

Upvotes: 3

Views: 1812

Answers (1)

mskuratowski
mskuratowski

Reputation: 4124

You can add it like:

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

Upvotes: 5

Related Questions