I am trying to write unit tests for a new project that I created, and I had a problem when I cannot understand how the class that I intend to write can actually be checked. I have simplified the class I'm trying to write below to give you an idea of โโwhat I'm trying to achieve.
So, I have an XML parser that will simply access the XML file from the given URL, retrieve the data I need and return it as an object. Thus, my code will look something like this (validation and population are not complete yet, but you get this idea):
public UserDetails ParseUserDetails(string request, string username, string password) { var response = new XmlDocument(); response.Load(string.Format(request + "?user={0}&password={1}", username, password));
My class is currently not being tested. I can't make fun of the load to throw a WebException to see how my class handles errors, and until I go through a valid URL, it will always throw an exception when I run tests against this class. I also cannot check the returned data from the class, since I cannot mock the XML document, since it is loaded from another URL.
I could break this down into a mocking object that extracts XML from a URL and calls it something like IXmlDocumentLoader, but later I come across the same problem where I have a class like this:
public class XmlDocumentLoader : IXmlDocumentLoader { public XmlDocument LoadXmlDocument(string request, string username, string password) { var response = new XmlDocument(); response.Load(string.Format(request + "?user={0}&password={1}", username, password)); return response; } }
This will make the ParseUserDetails method more reliable, but now the XmlDocumentLoader class is not tested. So I just moved the problem elsewhere? My question really is whether all classes should be testable, or do I not understand how unit testing is being tested?