Our team has just started unittesting and mocking, and we have run into some discussion considering extension methods. The question is what is a good approach to testing classes, that make use of extension methods. I.e. we have an Enum like this..
public enum State
{
[LangID(2817)]
Draft = 0,
[LangID(2832)]
Booked = 1,
[LangID(1957)]
Overdue = 2,
[LangID(2834)]
Checked = 3,
}
Which makes use of the extension method:
public static string GetDescription(this Enum _enum)
{
Type type = _enum.GetType();
MemberInfo[] memInfo = type.GetMember(_enum.ToString());
if (memInfo != null && memInfo.Length > 0)
{
object[] attrs = memInfo[0].GetCustomAttributes(typeof(LangID), false);
if (attrs != null && attrs.Length > 0)
return LanguageDB.GetString(((LangID)attrs[0]).ID);
}
return _enum.ToString();
}
Which again will be called by the class under test, like so ..
public class SUT(){
public void MethodUnderTest(){
string description = SomeObject.Status.GetDescription();//Status is Type State
}
}
In this example the enum is getting a description in the language of the user, via LanguageDB, which unfortunately is not injected inside the class since it's static. We could naturally refractor the lot, but this would be a big investment, considering the code is working almost flawless. Any good suggestion?