Meet EMo

19. March 2009

Just to see how doing an open source project would work Rob Cooper from Can’t grok won’t grok and me have started a small open source project. World, please meet EMo. EMo is a small component to make unit-testing events in .Net languages easier to write and easier to read.

Lets start with the simplest case. You want to test if a component under test has raised an event. How would you go about it. Right now the easiest way to do it would be to subscribe a lambda expression to the event, have it increment a counter or set a flag and test the flag. For an XUnit.Net test It would look something like this;

   1: [Fact]
   2: public void WithoutEMo()
   3: {
   4:     var raised = 0;
   5:     eventSource.NormalEvent += (sender, args) => raised++;
   6:  
   7:     eventSource.InvokeNormalEvent(null);
   8:  
   9:     Assert.True(raised > 0);
  10: }

Its not completely unreadable. But this simple case already has some trouble expressing intent. More complex cases will be even less readable.

But now with EMo you can do it like this:

   1: [Fact]
   2: public void NormalUsage()
   3: {
   4:     var eventRecorder = eventSource.Monitor("NormalEvent");
   5:  
   6:     eventSource.InvokeNormalEvent(null);
   7:  
   8:     eventRecorder.Raised().Any();
   9: }

 I admit, its a small improvement. But we’re talking about a very simple case here. Right now we’re working hard on getting more complex cases working. The next example already works too;

   1: [Fact]
   2: public void MoreComplicatedAssert()
   3: {
   4:     var eventRecorder = eventSource.Monitor("NormalEvent");
   5:  
   6:     eventSource.InvokeNormalEvent(null);
   7:     eventSource.InvokeNormalEvent(null);
   8:  
   9:     eventRecorder.Raised().WithParams(eventSource, null).Twice();
  10: }

Another thing we’re working on is getting the exception messages just right so to help you debug failing tests more easilly. To get your hands on the code you can get it from the svn repository at google code. I’m afraid there’s no downloadable binary yet and no documentation either. We’re still working on that too.

Comments are closed

Powered by BlogEngine.NET 1.5.0.7
Theme adapted from BlogEngine.NET standard theme by Mads Kristensen

Mendelt Siebenga

Mendelt Siebenga with coffeeMendelt Siebenga works as a C# programmer. In his spare time he's been known to pick up Python, Lisp and even a soldering iron from time to time.

You can also find me here