I know that the unit test should be isolated from any dependencies. I am trying to write unit tests for an application using Typemock. Issue is one of the methods in a class accepts a couple of Xml filepath parameters and then creates an XMLDocument object inside the method to be used somewhere in the method.
public bool ValidateXml(String aFilePath, String ruleXmlPath)
{
XmlDocument myValidatedXml = new XmlDocument();
try
{
myValidatedXml.Load(aFilePath);
}
catch
{
return false;
}
XmlDocument myRuleXml = new XmlDocument();
try
{
myRuleXml.Load(ruleXmlPath);
}
catch
{
return false;
}
MyClass myObject = new MyClass(myValidatedXml);
//Do something more with myObject.
XmlNodeList rules = myRuleXml.SelectNodes("//rule");
//Do something more with rules object.
return true;
}
How do I write a unit test for this without having to specify the physical location?
Note: I am not allowed to change the code unfortunately.
You could always create a temp Xml and pass the path of the temp file and then delete it after the Test is executed. In NUnit this can be easily accomplished using
[SetUp]and[TearDown]attributes.the
Setupmethod is executed before each test case and theTeardownmethod is executed after each test caseNote: For MSTest the
[Setup]and[TearDown]attribute can be replaced with[TestInitialize]and[TestCleanup]respectively. Thanks Cwan !