I'm trying to unit test a controller. SomeController inherits from BaseController. BaseController inherits from Microsoft.AspNetCore.Mvc.Controller.
My NUnit tests fail because a property Response in Controller is always null when running my tests. My test currently looks like this:
[Test]
public void GetByFilter_ShouldReturnABadRequest_WhenModelStateIsInvalid()
{
_sut.ModelState.AddModelError("SessionName", "Required");
var result = _sut.GetByFilter(_mockedFilter);
Assert.IsAssignableFrom<BadRequestObjectResult>(result);
}
_sut is set by [SetUp] with this method:
protected override void InitSut() => _sut =
new SomeController(_mockedLogger, _mockedService, _mockedRepository, _mockedHtmlSanitizer);
The code that fails on tests but works fine when running looks like this:
Response.Headers.Add("RowCount", result.Count().ToString());
return Ok(result);
Seems I need to do something to have the BaseController set the Response. In production the Response is filled with an instance of HttpDefaultResponse, but Response is readonly, so no way to set this myself.
I have tried following testing guides like this, but without luck:
https://learn.microsoft.com/en-us/aspnet/web-api/overview/testing-and-debugging/unit-testing-controllers-in-web-api
What do I need to do to start my controllers correctly and thus pass my tests?