我必须对一个方法 (runMethod()) 进行单元测试,该方法使用继承的抽象类中的方法来创建布尔值。抽象类中的方法使用 XmlDocuments 和节点来检索信息。代码看起来有点像这样(这非常简化,但它说明了我的问题)
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
}
}
}
因此,代码想要从创建的 XmlDocument 中获取一个属性,并使用它将结果形成一个布尔值。现在我的问题是,确保我可以控制布尔结果行为的最佳解决方案是什么。我正在使用 Moq 进行可能的模拟。
我知道这个代码示例可能有点模糊,但这是我能展示的最好的。希望大家能帮忙。
编辑:我基本上需要的是:
我需要能够在测试 AbstractInheret 类时控制 getProperty()