We have a controller that derives from ControllerBase with an action like this:
public async Task<ActionResult> Get(int id)
{
try
{
// Logic
return Ok(someReturnValue);
}
catch
{
return Problem();
}
}
We also have a unit test like this:
[TestMethod]
public async Task GetCallsProblemOnInvalidId()
{
var result = sut.Get(someInvalidId);
}
But ControllerBase.Problem() throws a Null Reference Exception. This is a method from the Core MVC framework, so I don't realy know why it is throwing the error. I think it may be because HttpContext is null, but I'm not sure.
Is there a standardized way to test a test case where the controller should call Problem()?
Any help is appreciated.
If the answer involves mocking: we use Moq and AutoFixtrue.
The null exception is because of a missing
ProblemDetailsFactoryIn this case the controller needs to be able to create
ProblemDetailsinstance viaSource
ProblemDetailsFactoryis a settable propertySource
that could be mocked and populated when testing in isolation.