"); string path = @"D:\Config.xml"; Syste" /> "); string path = @"D:\Config.xml"; Syste" /> "); string path = @"D:\Config.xml"; Syste"/>

How to shim XmlDocument.load using Microsoft Fakes

162 views Asked by At
    var xml = new XmlDocument();
    xml.LoadXml("<add name=\"console\" type=\"DefaultTraceLoader\" value=\"Error\"/>");

    string path = @"D:\Config.xml";
    System.IO.Fakes.ShimFile.ExistsString = p => true;

                    System.Xml.Fakes.ShimXmlDocument.AllInstances.LoadString = (a,b)=>
                    {
                        a = xDoc;
                        b = path;
                    };

System.Xml.fakes:

<Fakes xmlns="http://schemas.microsoft.com/fakes/2011/">
  <Assembly Name="System.Xml" Version="4.0.0.0"/>
  <ShimGeneration>
    <Add FullName="System.Xml.XmlDocument!"/>
  </ShimGeneration>
</Fakes>

I am writing a unit test case to shim xmldocument load method.when I debug the orginal project it not returning above xmldocument. am I not doing the right way for loadstring funciton to return the expected xml document in main project?

1

There are 1 answers

0
Sreenath On BEST ANSWER
ShimXmlDocument.AllInstances.LoadString = (a,b)=> 
{
    a = xDoc;
    b = path;
};

It would have worked as you expected if "a" was passed as ref parameter. It is not the case and hence you can't change the object to which it points outside the lambda function i.e. in the method under test.

You can achieve what you want by modifying lambda as below:

ShimXmlDocument.AllInstances.LoadString = (a,b)=>
{
   a.LoadXml("<add name=\"console\" type=\"DefaultTraceLoader\" value=\"Error\"/>");
};