Our team just started unittesting and taunting, and we came across some discussions using extension methods. The question is what is a good approach to testing classes that use extension methods. That is, 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, }
Uses 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 will again be called by the tested class, for example.
public class SUT(){ public void MethodUnderTest(){ string description = SomeObject.Status.GetDescription();
In this example, the enumeration gets a description in the user's language through LanguageDB, which, unfortunately, is not entered inside the class, since it is static. We could, of course, exaggerate this lot, but this would be a great investment, given that the code works almost flawlessly. Any good suggestion?
Bborg source share