Devlico.Us
CodeBetter.Com
RSS 2.0 via Feedburner
           Do you Twitter? Follow us @devlicious

Casey Charlton - Insane World

Hang the code, and hang the rules. They're more like guidelines anyway


Always One Step Ahead - Unit Test for Missing Attributes

Oren always amazes me - I think he is psychic!  Whenever I have a problem, I almost always find Oren blogged about it a few days before - and today's bugbear is a perfect example.

I started the day off with a really weird timeout exception in WCF. The code was just fine apparently, and I spent a very long time debugging code I was pretty sure was working. The error (in typical framework style) was not able to be caught, debugged, or otherwise detected - it just "happened" somewhere in the WCF call.

To cut a long story short ... it turned out an attribute was missing on a DTO class, and instead of WCF giving me a meaningful error, it just gave me a worthless message. One addition of [DataContract] later and all was working fine again.

So, it just happens that Oren (in association with Glenn) had done something similar, and so taking a leaf out of their book, here is a really basic unit test to check that the DTO classes have the right attributes. Some discussion on Oren's blog discussed whether unit tests were the right place for this kind of checking (as it is pretty much static code analysis), and in some ways I can see their point - however, frankly, a unit test is just as valid a way of testing the same thing, fits nicely into CI, and is in a common format to all the other checks.

[Test]
public void Ensure_DTO_classes_have_WCF_attributes()
{
  foreach (var type in typeof(ClassInDTOAssembly).Assembly.GetTypes())
  {
    if (type.IsInterface || type.IsAbstract)
        continue;
    if (type.Namespace.StartsWith("Interfaces.DTO"))
    {
       var attributes = type.GetCustomAttributes(
typeof (DataContractAttribute), true); if (attributes.Length == 0) Assert.Fail("Unable to identify [DataContract] attribute on type "
+ type.Name + " in DTO namespace"); else foreach (var memberInfo in type.GetMembers()) { if (memberInfo != null) { var memberAttributes =
memberInfo.GetCustomAttributes(typeof (DataMemberAttribute), true); if (memberAttributes.Length == 0) Assert.Fail("Unable to identify [DataMember] attribute on member "
+ memberInfo.Name + " on type "
+ type.Name + " in DTO namespace"); } } } } }


Comments

Reflective Perspective - Chris Alcock » The Morning Brew #88 said:

Pingback from  Reflective Perspective - Chris Alcock  » The Morning Brew #88

# May 7, 2008 2:26 AM

Christopher Bennage said:

LOL. I wrote this same test awhile back.  Though like a narcoleptic sloth I didn't bother to post about.  It's really cool (affirming) to see others arriving at the same solution.

Regarding whether or no a "unit" test is the right place for this: I certainly wouldn't call it a "unit" test, but I definitely want it as part of my automatic tests (specs). There's lots of things that aren't unit tests that I want in my test suite.

# May 19, 2008 10:27 AM

always 2 ahead said:

Pingback from  always 2 ahead

# May 30, 2008 4:13 PM

Leave a Comment

(required)  
(optional)
(required)  

Enter the numbers above:
Add

About Casey Charlton

A somewhat passionate and opinionated developer, with occassional sparks of wisdom, and occasional useful information. Check out Devlicio.us!

Our Sponsors

Red-Gate!