I have a following method which takes a XDocument, iterates through the nodes and remove/replace nodes based on some condition.
public static void Format(XDocument xDocument)
{
foreach(XmlNode documentNode in xDocument)
{
XmlNode[] spanNodes =documentNode.SelectNodes("//span") ;
foreach(XmlNode spanNode in spanNodes)
{
if(spanNode .Attributes["class"]!=null
&& !string.IsNullOrEmpty(spanNode.Attributes["class"].value)))
{
string styleName = spanNode.Attributes["class"].value;
string styleActionMapping = GetActionMappingForStyle (styleName);
switch (styleActionMapping)
{
case StyleActionMapping.Remove
RemoveSpanNode(spanNode);
break;
case StyleActionMapping.ReplaceWith
ReplaceSpanNode(spanNode);
break;
case StyleActionMapping.Keep
break;
}
}
}
}
}
Now I want to design unit tests in VS 2010 for the above method. I have the sample input and expected output in a store (XML/database) and I am thinking of passing the input data to the functions and matching their output with the expected output. My question is should I write only one [TestMethod] for Format(XDocument) or should I write one each for RemoveNode() and ReplaceNode(). Writing only one [TestMethod] for Format() is easy but I am not sure If this violates the principle of unit testing in which one should test only one thing at a time (and many things are happening in that Format() method). Also, I am not sure If I choose to test ReplaceNode() and RemoveNode(), how do I write the test methods for them i.e. what data I should pass to those methods. Could anybody give me any directions?
Should the test methods be like the following :-
[TestMethod]
CheckExpectedOutput_OnRemove(XDocument document)
{
/*
1) document has data only for remove
2) call Format() and get the output
3) Check the output in the step 2 and match with the expected output
*/
}
[TestMethod]
CheckExpectedOutput_OnReplace(XDocument document)
{
/*
1) document has data only for replace
2) call Format() and get the output
3) Check the output in the step 2 and match with the expected output
*/
}