I am writing to test an intricate class with complex nested calculations. We are looking at ActionMethod, and its returnType. The returnType is a complicated equation, how would I mock it?
var methodInfoMock = new Mock<MethodInfo>();
var actionModel = new ActionModel(methodInfoMock.Object, new List<object>(){});
We know how to mock ActionModel, but not its returnType. So we keep it as its own variable.
If we don't know how to mock a complicated calcuation, is it Better to just keep as Own Variable, or Member of a Class?
public void AddProducesResponseTypeAttribute(ActionModel action, Type returnType, int statusCodeResult)
{
if (returnType != null)
{
action.Filters.Add(new ProducesResponseTypeAttribute(returnType, statusCodeResult));
}
else if (returnType == null)
{
action.Filters.Add(new ProducesResponseTypeAttribute(statusCodeResult));
}
}
}
See equation for returnType Below,
foreach (ActionModel action in controller.Actions)
{
Type returnType = null;
if (action.ActionMethod.ReturnType.GenericTypeArguments.Any())
{
if (action.ActionMethod.ReturnType.GenericTypeArguments[0].GetGenericArguments().Any())
{
returnType = action.ActionMethod.ReturnType.GenericTypeArguments[0].GetGenericArguments()[0]
;
Anyways, we have the test working, just returnType is hanging out there on its own.
Final Result:
[Theory]
[InlineData(200, typeof(IActionResult))]
[InlineData(500, typeof(IActionResult))]
public void TestAddProducesResponseType(int expectedStatusCode, Type returnType)
{
// Arrange
var provider = new ProduceResponseTypeModelProvider();
var methodInfoMock = new Mock<MethodInfo>();
var actionModel = new ActionModel(methodInfoMock.Object, new List<object>() { });
// Act
provider.AddProducesResponseTypeAttribute(actionModel, returnType, expectedStatusCode);
// Assert
actionModel.Filters.ShouldContain(new ProducesResponseTypeAttribute(returnType, expectedStatusCode));
}