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
Confidence in your code

First, I highly recommend Kent Beck's Test Driven Development: By Example.  It's an excellent book.

I mentioned Beck's book, because it is there that I gained an insight affecting the way I'm writing unit tests.  (So Christopher, you're saying that one of the seminal works on unit testing has affected the way you are writing tests?  Um, yes, I am saying that.)

Kent asks a question about when you should remove duplicate or overlapping tests.  His answer was that if it doesn't decrease your confidence in the code, then it's okay to delete it.  (I hope I'm getting that right, my copy is loaned out at the moment.)

My current project uses WCF to handle communication from the desktop client to the server.  We have a set of interfaces for our services shared in a common assembly.  When I create a new interface, I always forget to decorate it with the ServiceContract attribute, or I'll create a DTO and forget the DataContract/DataMember attributes, or (and here's the worst) I'll forget to add the PrincipalPermission attribute to the actual implementation of our services!

This has been bothering me for a month or two, and finally it dawned on me: why don't I write tests for all of this?  I'm not confident in the code, I'm fearful that I'm forgetting things; let's write tests and remove the doubt!

Here's what one of the tests looks like:

public class Service_interfaces
	private const string ASSEMBLY = "SomeAssembly";
	private const string SERVICE_NAMESPACE = "ServiceInterfaces";

	public void must_have_a_ServiceContract_attribute()
		bool atLeastOne = false;

		foreach (Type type in Assembly.Load(ASSEMBLY).GetTypes())
			if (type.Namespace.Contains(SERVICE_NAMESPACE))
				atLeastOne = true;

				object[] attributes = type.GetCustomAttributes(typeof (ServiceContractAttribute), false);
				Assert.IsTrue(attributes.Length > 0,
							  string.Format("The {0} does is not decorated with the ServiceContractAttribute", type));

					  string.Format("No service interfaces were found in {0} in the namepace {1}.",

So I am just the last on the bus?  What do you think?  Is there a better way to handle this? 

Posted 08-22-2007 10:53 PM by Christopher Bennage
Filed under: ,


About The CodeBetter.Com Blog Network
CodeBetter.Com FAQ

Our Mission

Advertisers should contact Brendan

Google Reader or Homepage Latest Items
Add to My Yahoo!
Subscribe with Bloglines
Subscribe in NewsGator Online
Subscribe with myFeedster
Add to My AOL
Furl Latest Items
Subscribe in Rojo

Member Projects
DimeCasts.Net - Derik Whittaker

Friends of
Red-Gate Tools For SQL and .NET


SmartInspect .NET Logging
NGEDIT: ViEmu and Codekana
NHibernate Profiler
Balsamiq Mockups
JetBrains - ReSharper
Web Sequence Diagrams
Ducksboard<-- NEW Friend!


Site Copyright © 2007 CodeBetter.Com
Content Copyright Individual Bloggers


Community Server (Commercial Edition)