I’m having a little bit of a dilemma. I have a very basic class with functions returning specific XPath query results.
Here is the code I’m currently using.
[TestFixture]
public class MarketAtAGlance_Test
{
private XmlDocument document;
private MarketAtAGlance marketAtAGlance;
[SetUp]
public void setUp()
{
this.document = new XmlDocument();
// load document from file located in the project
this.marketAtAGlance = new MarketAtAGlance(document);
}
[Test]
public void getHourlyImport_Test()
{
Assert.AreEqual(100.0d, marketAtAGlance.getHourlyImport());
}
[Test]
public void getHourlyExport_Test()
{
Assert.AreEqual(1526.0d, marketAtAGlance.getHourlyExport());
}
}
public class MarketAtAGlance
{
XmlDocument document;
public MarketAtAGlance(XmlDocument document)
{
this.document = document;
}
public double getHourlyImport() {
double value = Convert.ToDouble(document.SelectSingleNode("//information[@id=\"dat11\"]/new_val").InnerText);
return value;
}
public double getHourlyExport() {
double value = Convert.ToDouble(document.SelectSingleNode("//information[@id=\"dat12\"]/new_val").InnerText);
return value;
}
}
This is my first use of unit testing so I’m still unsure of many minor things. As you can see, I’m loading a static XML file located on my hard drive. Should I have the extra dependency or put the XML text in a big string? I’m loading an older XML file (with the same format) because I can test with already known values.
Also, how would I go about unit testing an XmlHttpReader (class that takes in an XML url and loads it as a document?
Any comments on my question or comments about the design?
I would construct the XML in the test setup, but limit the XML to only what you need for the test to pass. It looks like your XML document could be very simple in this case.
That XML would pass your test.
I also wouldn’t test the XmlHttpReader, if that is a system class. You could mock a dependency to it. You might need to wrap it with something to help you easily decouple it as dependency from your class.