I have a controller and I need to unit test it. The controller defines the following constructor:
public class ControllerName : AnotherController
{
public ControllerName(
IWebHostEnvironment environment,
ILogger<ControllerName> logger,
IHttpContextAccessor httpContext)
: base(environment, logger, httpContext)
{
}
...
}
The Controller inherit AnotherController, who sets:
public readonly IWebHostEnvironment _environment;
public readonly ILogger _logger;
public readonly IHttpContextAccessor _httpContext;
public AnotherController(
IWebHostEnvironment environment,
ILogger<AnotherController> logger,
IHttpContextAccessor httpContext)
{
_logger = logger;
_environment = environment;
_httpContext = httpContext;
if (_httpContext == null)
throw new NullReferenceException("_httpContext -> NullReferenceException");
}
The two controllers are in different projects.
I've searched and couldn't find a way to instantiate the first controller in my test class because of the Dependency Injection.
Does anyone know how to do it?
I'm using .NET 5.