Ye Olde 2.0 Code

Yes, I am so outdated.
I am to speak about C# 3.0 (or is it 3.5?) or even 4.0 features, not stale stinky legacy 2.0 code. But well, I am into a Moss + Commerce Server project so until I am given the time to explore how it'll behave into a 3.5 environment I will have lots of this (not that I am complaining).
The piece of functionality I am going to talk about is a testing pseudo-framework I created in an inspired morning to mitigate some of the pains we will have (somebody call it development, go figure).
After there nice post regarding my motivations to create that piece of code, we are left with a bunch of DTOs decorated with attributes.
Do we have to unit test DTOs? Some will say there's no point. Some other will say that it we can do it with close to zero effort... why not? I am one of those.
So I was very happy when I found Class Tester. Basically it can test that your constructors and your properties have the simplest get-set behavior and that you did not screw up during copy&pasting phase. Its raison d'etre is mitigated by 3.5 automatic properties but... didn't you notice the big 2.0 in the TITLE?.
The framework has its limitations but those are happily overlooked with exclusion lists. It also suffers from "reflective strings invasion" ® and ™ and © (eat that, thoughtful lawyers!!) and I plan to add some 3.5 expression trees magic around it when I have time to fight that disease. My framework does very few things, but heck you can't ask a lot from DTOs!!:
  • test that primitive properties behave as they should
  • test that non-primitive properties exhibit the same behavior
  • check that both the implemented class and the properties are decorated with the right attributes
By the way I consider a primitive property a property which type is primitive (string, Guid, int,...) and non-primitive one is mostly that one which type is another DTO (interfaces all over the place)
Coolness that motivates this post comes from the fact that with a very simple generic helper all those wrapper classes are tested in less than 2 minutes. Basic methods are:
  • Properties_BasicBehavior: a mere wrapper to set the ignored properties to the PropertyTester
  • Properties_NonBasicBehavior: a nice way of using a KeyValuePair<T, K> in which all the properties along with its type get a little help from RhinoMocks' MockRepository.GenerateStub() to generate a value
  • Properties_DecoratedWithProfileAttribute: in which, using List<>.Foreach and an Action<T> anonymous delegate, the existence of the attribute and the membership of the property to an exclusion list are asserted in one go. Here is the snippet:
Nice, ain't it?