Hi,
You can fake the XML document and children using similar code:
[TestMethod]
public void TestXMLAttributes()
{
// Arrange
var root = Isolate.Fake.Instance<XmlElement>();
var elementOne = Isolate.Fake.Instance<XmlElement>();
Isolate.WhenCalled(() => elementOne.Attributes[0].Value).WillReturn("Value1");
var elementTwo = Isolate.Fake.Instance<XmlElement>();
Isolate.WhenCalled(() => elementTwo.Attributes[0].Value).WillReturn("Value2");
Isolate.WhenCalled(() => root.ChildNodes)
.WillReturnCollectionValuesOf(new[] { elementOne, elementTwo });
// Act
var actualAttributes = new List<string>();
foreach (XmlNode childNode in root.ChildNodes)
{
actualAttributes.Add(childNode.Attributes[0].Value);
}
// Assert
CollectionAssert.AreEquivalent(new []{"Value1", "Value2"}, actualAttributes);
}
In this example we created an XML element, faked its children list and set child attributes behavior to return specific values.
I am not sure what exactly are you trying to test in your code. Since XML is easy to create data object, it may be easier to use real XML document instead of fake. It will also make the test more readable and less fragile. If you're not verifying anything against the XML there is no must to fake it.
Regards,
Elisha
Typemock Support