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
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:

[TestFixture]
public class Service_interfaces
{
	private const string ASSEMBLY = "SomeAssembly";
	private const string SERVICE_NAMESPACE = "ServiceInterfaces";

	[Test]
	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));
			}
		}

		Assert.IsTrue(atLeastOne,
					  string.Format("No service interfaces were found in {0} in the namepace {1}.",
									ASSEMBLY,
									SERVICE_NAMESPACE));
	}
}

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: ,

[Advertisement]

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)