Welcome to EMC Consulting Blogs Sign in | Join | Help

Simon Evans' Blog

My blog covers the technology areas I focus on here at EMC Consulting, namely Architecture using the .NET Framework, ASP.NET, WCF, WCF Data Services and Windows Azure Follow me on twitter @simonevans

Design pattern for writing unit tests with event driven architectures

Introduction

Unit testing a great way of ensuring quality in your class method calls. It gives you confidence that as you refactor your code, all works as you originally intended. Following the test driven development approach to writing your unit tests before you write your code leads to both better domain knowledge, better quality code and a more usable API for calling clients to use.

Recently I have been working on a project whose design goals were best achieved through an event driven programming model. An event driven pattern most closely matched the way in which the business worked in real life, and the results have been a very lean and effective model for the business to use.

I wanted to write unit tests to check that the events had fired as expected when a particular method was called, with certain arguments passed in. It was vital that these events were working as expected, as the whole behavior of the application would be effected if the event model did not function properly.

The problem

NUnit (and other variants of) allow you to write unit test methods, either expecting the code to run without exception, or, the method to return an expected exception. You write an Assert statement, usually to check that a condition is true (such as a method return). So how you check that a single method call has fired all the events you expect? The method may return without exception, but that doesn’t tell you that the events have been fired as a result.

Pair programming to get to the answer

I came across the answer to my problem during a pair programming session with one of my team members. I am mentioning this, because I am certain I would not have been able to solve the problem so elegantly if I had been on my own; stepping back from the keyboard allowed me to see the problem much more clearly.

The solution

The pattern to the solution is shown in the diagram below:

It involves providing a extra layer of indirection between the NUnit test fixture client (shown on the left) and the business logic class (MyBusinessProcess). To facilitate code reuse, the pattern uses a delegate which can be evaluated (using the CheckProcessEvents method) to check for Boolean values which indicate whether each event has been fired as expected.

The order of play is:

  • NUnit calls DoUnitTest on the test fixture class
  • DoUnitTest calls test unit process DoProcess method, which has its events wired up for MyBusinessProcess evemts
  • The DoProcess method calls ExecuteProcess method on MyProcess which in turn fires some of its events.
  • The DoUnitTest method delegates evaluating the business process events to the CheckProcessEvents method method, and Asserts the unit test if the events run as expected.
Published 06 June 2005 05:55 by simon.evans
Filed under:

Comment Notification

If you would like to receive an email when updates are made to this post, please register here

Subscribe to this post's comments using RSS

Comments

 

Business Consultant said:

This is one of the blogs I visit very regualrly. I always get a lot of inforamtive stuff . In addition to the article My comment would be  that based upon my experience of 10 years  at EMC Consulting (previously Conchango), its never been dull and there has always been something interesting or unusual thrown into the mix of things so you don’t do the same old tasks all the time.

October 2, 2009 10:29
 

Business Management said:

Even powerful ideas can lose their potency when we lose sight of how to use them effectively. For example, you may understand the potential power of the questions you ask in sales situations. But are you really asking questions that help you?

October 5, 2009 10:23
 

web desing said:

In my opinion this tactic worked and rarely, if ever, ended up with me being in the awkward position of explaining why I applied for a job that I didn't really want. It also was a way for me to practice conversations with recruiters. I could try out different answers to the usual questions of why I'm looking for another position and how much I would like to be paid. It's a lot easier to do such things when you've got a lot of leads and aren't desperate for employment.

October 7, 2009 05:07
 

Graphic design said:

According to research from Harvard Business School, the business consulting industry generates about $100 billion in annual revenues from U.S. consultants. Especially in a slow economy, companies need business consultants to help them increase revenues and cut costs.

October 15, 2009 07:45

Leave a Comment

(required) 
(optional)
(required) 
Submit

About simon.evans

Simon is a Managing Consultant for Conchango in the UK, part of EMC Consulting Services. He is an expert in .NET development, and more specifically in WCF and ASP.NET, having participated in several Microsoft early adoption programs. Simon believes deeply that a broad understanding of key technology concepts is an essential foundation to being a gifted designer and builder of solutions.
Powered by Community Server (Personal Edition), by Telligent Systems