Testing a method used from an abstract class
- by Bas
I have to Unit Test a method (runMethod()) that uses a method from an inhereted abstract class to create a boolean. The method in the abstract class uses XmlDocuments and nodes to retrieve information. The code looks somewhat like this (and this is extremely simplified, but it states my problem)
namespace AbstractTestExample
{
public abstract class AbstractExample
{
public string propertyValues;
protected XmlNode propertyValuesXML;
protected string getProperty(string propertyName)
{
XmlDocument doc = new XmlDocument();
doc.Load(new System.IO.StringReader(propertyValues));
propertyValuesXML= doc.FirstChild;
XmlNode node = propertyValuesXML.SelectSingleNode(String.Format("property[name='{0}']/value", propertyName));
return node.InnerText;
}
}
public class AbstractInheret : AbstractExample
{
public void runMethod()
{
bool addIfContains = (getProperty("AddIfContains") == null || getProperty("AddIfContains") == "True");
//Do something with boolean
}
}
}
So, the code wants to get a property from a created XmlDocument and uses it to form the result to a boolean. Now my question is, what is the best solution to make sure I have control over the booleans result behaviour. I'm using Moq for possible mocking.
I know this code example is probably a bit fuzzy, but it's the best I could show. Hope you guys can help.