I’ve often had to use XML documents in my unit testing. In the past, I’ve always done a concatenation to string together a document.
However, I came across a post on Haacked that lead me to a better solution, embedded resources. Basically, you add a document to your project and mark it as an “Embedded Resource” rather than “Content” in the “Build Action” attribute.
This will pack it into the unit testing dll and allow you to extract it from the assembly. No more missing documents causing failing tests! In my custom class, I’ve defined a method for unwrapping the resource that allows you to pass in arguments that it will use for customizing the xml nodes. I found this to be simpler than attempting to navigate the document and update the nodes although I’m guessing it may be less performant.
One thing to be sure of with this, is that you use a centralized location for the resources so that your lookup doesn’t fail. So now I’m able to write my tests such that their intent is clear and the code is clean.
Although I haven’t had the need yet, I could see where this could easily grow into handling multiple types of documents but I’ll hold off on that code until I need it. 🙂