chevron-thin-right chevron-thin-left brand cancel-circle search youtube-icon google-plus-icon linkedin-icon facebook-icon twitter-icon toolbox download check linkedin phone twitter-old google-plus facebook profile-male chat calendar profile-male
0 votes
Hello,

I need to mock the following code:

XmlDocument document = new XmlDocument();
document.Load(this.XmlFile); //XmlFile = path to file

What I want to do is have TypeMock return an XmlDocument with preset XmlDocument object that uses LoadXml, and a hard-coded string I provided it.

How can I do this? Do I have to mock the creation of the object and return my object, then prevent the Load call?

Thanks.
asked by bmains (13.2k points)

1 Answer

0 votes
Hi,

Basically yes, an easy way to do it would be to mock out the creation of the document and all other calls made on him.
for example the following code will do that and mock the call to the Load method.
using (RecordExpectations rec = new RecordExpectations())
{
    XmlDocument document = new XmlDocument();
    document.Load("Some Path"); 
    //... mock the rest of the calls made on document
}

Another way, that may be possible depending on your exact implementation, is to create a "fake" XmlDocument that will contain the exact data you need for the test, and "inject" it instead of the the normally created one.
For example if the class using the document acess is through a "GetDocument" or something similar, you can mock that getter and return the "fake".
answered by lior (13.2k points)
...