I saw the very same issue in this post but none of the solutions works now in ASP.Net Core 2.2. When I debug the unit test the Response property is still null and thus the test fails.
I have been reading the asp.net core docs for an answer how to mock the ControllerContext
so that the Response property has a value but I couldn't find anything working.
Here is the line inside the action that makes troubles:
Response.Headers.Add("Access-Control-Expose-Headers", "Content-Range");
So what I have ended up with in the unit test set up is:
var routeData = new RouteData();
routeData.Values.Add("controller", "Home");
var headerDictionary = new HeaderDictionary();
var response = new Mock<HttpResponse>();
response.SetupGet(r => r.Headers).Returns(headerDictionary);
var httpContext = new Mock<HttpContext>();
httpContext.SetupGet(a => a.Response).Returns(response.Object);
var actionContext = new ActionContext(
httpContext.Object,
routeData,
new ControllerActionDescriptor());
_controller.ControllerContext = new ControllerContext(actionContext);