I have an extension method for a helper class in an MVC4 project:
public static class ExtensionMethods
{
public static object Value<TModel, TProperty>(this Expression<Func<TModel, TProperty>> expression, ViewDataDictionary<TModel> viewData)
{
return ModelMetadata.FromLambdaExpression(expression, viewData).Model;
}
}
This does something very simple in a manner that is anything but simple.
So, how can this best be unit tested? It would be preferable to avoid mocking static methods or using dependency injection, but I am open minded if these really are the only viable approaches in this case.
Is this just a flawed design that could be improved so as to be more amenable to unit testing?
This is indeed a flawed design and is presumably common. The correct method signature requires the parameter order to be switched:
This can then be called from unit tests: