views:

72

answers:

2

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?

+1  A: 

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.

<someRoot>
   <someNode>
        <information id='dat11'><new_val>100.0</new_val></information>
        <information id='dat12'><new_val>1526.0</new_val></information>
   </someNode>
 </someRoot>

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.

Mark
+1  A: 

For your first question, whether you should have a big XML string or load it from a file. I would say either works. To be honest though since you are loading from a file inside the project I would keep it in the project as an embedded resource and load it via reflection. That would take the mess of file structure out of the picture if any of your colleages run it from their pc's. The only best practice I've really encountered with Unit Tests is to make sure you're testing properly and make sure others can run the test easily.

For your second question, about the XmlHttpReader. It would depend on your output. If you can test that you have valid XML then go for it. I would reccommend negative testing as well. Point it to http://stackoverflow.com, or a URL you know will error out and decorate the test with the appropriate expected error.

Alexander Kahoun