So, here I am being the good developer and attempting to put together a series of unit tests for a project (the generic data validation engine is actually very close!). I have always appreciated the concept of unit testing and I have no problem doing it for simple little test cases. Where it always tends to get left by the road side is when I run into one of those more complex scenarios that does not fit the mold.
There was just such an occasion last week and I still have not found a way around it – at least one that I like. So, here is the scenario. I have an object whose init method accepts an optional path to an XML configuration file and if received, parses that configuration file into an internal data structure which is a fairly complex series of nested structures and arrays. All is well and good until I went to write a couple of unit tests for this …. I know I should have written them first, but at least they are being written!
The problem is, as part of this XML parsing/conversion process, the object in question is generating unique identifiers and inserting them into the data structure. These unique identifiers are UUIDs, so you can probably start to see the issue already. The way I was going about testing this piece functionality was to pass in an example XML file to the init method, capture the resulting data structure, and then compare it against a manually created data structure. However, as the conversion process generates unique identifiers on the fly, I can’t mimic those exact same identifiers in my manually created data structure and thus the test fails.
One suggestion I had was to rewrite the process of creating these identifiers so that if I passed in a test flag, then it would generate the identifier in a way that could be replicated. While this could work, the fact that I am having to go back and introduce more logic and bend my code just for testing purposes does not sound right.
So, before I toss unit testing aside once again, does anybody have any bright ideas on how you might test functionality like this?