Mocking with Rhino Mocks

Friday, May 16, 2008 10:29 PM

Ok, myself and a couple of colleagues spent a ton of time today scratching our heads because of an odd behavior in Rhino Mocks. Basically, we set an expectation, set the expectation to repeat only once, and then were surprised to see a passing test when we knew the actual code was calling the expectation too many times. So we created the below example. The test in the example should fail, but it passes.

using NUnit.Framework;
using Rhino.Mocks;

namespace UnitTests
{
   public interface IMyTestInterface
   {
      void MyTestMethod();
   }

   [TestFixture]
   public class RepeatTests
   {
      [Test]
      public void Test()
      {
         var mockery = new MockRepository();
         var myMock = mockery.DynamicMock<IMyTestInterface>();

         using (mockery.Record())
         {
            myMock.MyTestMethod();
            LastCall.Repeat.Once();
         }
         using (mockery.Playback())
         {
            myMock.MyTestMethod();
            myMock.MyTestMethod();
         }
      }
   }
}

Now to do some searching and querying to figure out what we're doing wrong.

Tags: .net, tdd

Throwing Exceptions

Thursday, May 1, 2008 8:56 AM

I wanted to capture this in case I ever find myself trying to present this concept again and am failing to come up with a simple example that works. It's about exception handling in C#. I've known about this issue for quite some time and tried to present the problem to an internal user group where I work. Unfortunately the example I came up with didn't provide the expected result and we spent the whole presentation floundering around with the damn thing. Kudos to a buddy of mine for taking the time one weekend to come up with this working version (Thanks Paul).

Basically, when you catch exceptions, the way you re-throw them has a very important impact on the stack trace of the exception when it is caught later. Below is the code in its entirety. I'm mostly putting it here for reference, but would be glad to answer any questions you may have.

using System;

namespace ThrowSpike
{
   class Program
   {
      static void Main()
      {
         try
         {
            Console.WriteLine("ThrowOnly...");
            ThrowOnly();
         }
         catch (Exception ex)
         {
            Console.WriteLine(ex); // See a good stack trace
         }

         try
         {
            Console.WriteLine("ThrowCaughtException...");
            ThrowCaughtException();
         }
         catch (Exception ex)
         {
            Console.WriteLine(ex); // See an bad stack trace
         }
      }

      private static void ThrowOnly()
      {
         try
         {
            ThrowException();
         }
         catch
         {
            throw;
         }
      }

      private static void ThrowCaughtException()
      {
         try
         {
            ThrowException();
         }
         catch (Exception ex)
         {
            throw ex;
         }
      }

      private static void ThrowException()
      {
         throw new ApplicationException("This is a test");
      }
   }
}
Tags: .net

Current Expectations

Wednesday, April 30, 2008 8:20 PM

Welcome,

I thought I'd use my first post to set expectations. This is as much for my benefit as it is for anyone who might stumble across my blog.  It's amazing how the act of writing something down really forces us to solidify the idea.

My current intention for this blog is to use it as a place to occasionally come and capture some kind of thought that I either want to share or remember.  Either way, I want that thought to be available to anyone who might also be interested.

The funny thing right now (as I'm writing this) is how many versions of that sentence I've gone through.  I kind of wish I had kept them all!  I would capture an intention and start writing a bit about it to kind of further or clarify it.  Then I would realize that I could just alter the sentence and not need the clarification.

I would like to draw attention to two specific points in the intention though:

"My current intention..."

That means exactly what it says – current.  This may be what I was thinking going in, but there's nothing that says my focus won't completely shift at some point.  I would expect, however, for it to remain fairly consistent as this seems to me a reasonable intention for one's blog.  The main thing that might change is the next thing.

"... occasionally ..."

I don't currently have any intention of regularly updating the blog.  It seems like many people have a metric they shoot for like X number of posts per month or week or something.  I only intend to use this is a medium for capturing thoughts that I want to share or remember.  And that's only the thoughts that I feel strongly enough about to take the time to write them down in some kind of meaningful manner.  I have a lot of trouble with time management currently between family, career, gaming (WoW currently), and reading.  I really don't want to add an additional item to the mix right now.  There's something to be said for "Quality over Quantity" and I seem to be walking a fine line between the two already – mostly because of the gaming is my thought.

Well, that's it for now.  I feel like writing this post was a great start because it really caused me to nail down my thoughts around why I bothered to create a blog and what I hope to get out of it.

Happy coding!

Tags: