Images in this post missing? We recently lost them in a site migration. We're working to restore these as you read this. Should you need an image in an emergency, please contact us at imagehelp@codebetter.com
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"); } } } } }

Posted 05-06-2008 9:38 AM by Jak Charlton
Filed under: , , ,

[Advertisement]

Comments

Reflective Perspective - Chris Alcock » The Morning Brew #88 wrote Reflective Perspective - Chris Alcock » The Morning Brew #88
on 05-07-2008 2:26 AM

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

Christopher Bennage wrote re: Always One Step Ahead - Unit Test for Missing Attributes
on 05-19-2008 10:27 AM

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.

always 2 ahead wrote always 2 ahead
on 05-30-2008 4:13 PM

Pingback from  always 2 ahead

About The CodeBetter.Com Blog Network
CodeBetter.Com FAQ

Our Mission

Advertisers should contact Brendan

Subscribe
Google Reader or Homepage

del.icio.us CodeBetter.com Latest Items
Add to My Yahoo!
Subscribe with Bloglines
Subscribe in NewsGator Online
Subscribe with myFeedster
Add to My AOL
Furl CodeBetter.com Latest Items
Subscribe in Rojo

Member Projects
DimeCasts.Net - Derik Whittaker

Friends of Devlicio.us
Red-Gate Tools For SQL and .NET

NDepend

SlickEdit
 
SmartInspect .NET Logging
NGEDIT: ViEmu and Codekana
LiteAccounting.Com
DevExpress
Fixx
NHibernate Profiler
Unfuddle
Balsamiq Mockups
Scrumy
JetBrains - ReSharper
Umbraco
NServiceBus
RavenDb
Web Sequence Diagrams
Ducksboard<-- NEW Friend!

 



Site Copyright © 2007 CodeBetter.Com
Content Copyright Individual Bloggers

 

Community Server (Commercial Edition)