C# 如何在VS 2013中使用Microsoft Fakes填充XmlAttribute
我有下面的代码,我正在测试C# 如何在VS 2013中使用Microsoft Fakes填充XmlAttribute,c#,unit-testing,microsoft-fakes,C#,Unit Testing,Microsoft Fakes,我有下面的代码,我正在测试 public Loader(XmlAttributeCollection attributes) { if (attributes == (XmlAttributeCollection)null) throw new ArgumentNullException("attributes"); foreach (XmlAttribute attribute in attributes)
public Loader(XmlAttributeCollection attributes)
{
if (attributes == (XmlAttributeCollection)null)
throw new ArgumentNullException("attributes");
foreach (XmlAttribute attribute in attributes)
{
if (attribute.Name.Equals("name", StringComparison.OrdinalIgnoreCase))
{
name = attribute.Value;
}
else if (attribute.Name.Equals("type", StringComparison.OrdinalIgnoreCase))
{
loaderType = attribute.Value;
}
else
{
loaderAttributes.Add(attribute.Name, attribute.Value);
}
}
}
是否可以填充attribute/attributecollection,以便可以返回想要返回的属性对象
例如,我想返回两个xmlattribute对象,一个属性名为“name”,另一个属性名为“type”,以及它们各自的值
我的意思是类似于下面的shim语句,在该语句中,我可以返回一个具有名称和值的xmlattribute对象,而不是返回null吗
ShimXmlAttributeCollection.AllInstances.ItemOfGetInt32 = (x, y) =>
{
return null;
};
这是我的示例xml
<add name=\"console\" type=\"DefaultTraceLoader\" value=\"Error\"/>
您可以:
var xml = new XmlDocument();
xml.LoadXml("<add name=\"console\" type=\"DefaultTraceLoader\" value=\"Error\"/>");
var enumerator = xml.DocumentElement.Attributes.GetEnumerator();
您必须伪造
GetEnumerator()
方法。我这台电脑上没有假货,所以我不能告诉你怎么。。。。但是只要返回一个新的XmlAttribute[]{your elements}.GetEnumerator()
就足够了。这是一个很好的建议。但是GetEnumerator()方法在StubXmlAttributeCollection或ShimXmlAttributeCollection中不可用。@krishna,因为它是在XmlNamedNodeMap
中定义的。。。尝试查看StubXmlNamedNodeMap
Ok。我使用了它,它可以作为ShimXmlNamedNodeMap提供。它也很有效。但是,在返回GetEnumerator时,是否可以直接在ShimXmlNamedNodeMap中传递我的xml内容?是否不可能存根此XmlDocument?如果有可能的话,那就太好了。@krrishna你是什么意思?我被要求不要直接使用XmlDocument,而是被要求将它存根。那么有可能吗?@krrishna我不明白您是否不想在单元测试中执行XmlDocument
/LoadXml
,或者您是否想创建一个使用接口/虚拟方法的XmlDocument
替换,然后可以对其进行存根之类的操作
ShimXmlNamedNodeMap.AllInstances.GetEnumerator = x =>
{
return enumerator;
};