Rhino MockThinking about next version...

time to read 3 min | 561 words

I finally got the excuse to work on Rhino Mocks again :-)

I've a line in one of my presenters that looks like this:

IView[] toSave, unsaved = (IView[]) unSavedList.ToArray(typeof (IView));

Unfortantely, when I pass mocked IViews to this method, the following exception is raised: "System.InvalidCastException : At least one element in the source array could not be cast down to the destination array type."

The issue seems to be that Rhino Mocks is uing remoting proxies to do its work, and that doesn't sit well with Array.Copy(). So now I got two choices, I can modify the production code* so it would work with remoting proxies (basically avoiding the ArrayList.ToArray() and the Array.Copy() methods). Or, I can rip the guts out of Rhino Mocks and replace it with something that I like better. The two options I see now are:

  • either, use Castle.DynamicProxy.
  • or, write my own proxies using Reflection.Emit.

Pros for Castle.DynamicProxy:

  • It's nearly literally two lines of code.
    ProxyGenerator generator = new ProxyGenerator(); 
    return generator.CreateProxy(destinationType,new RhinoInterceptor(),source);
  • I'll be using tried and tested library with quite a bit of support.
  • I can get it out nearly instantly.

Cons for using Castle.DynamicProxy:

  • It means another library to carry with Rhino.Mock.

Pros for rolling my own:

  • It will teach me quite a bit about Reflection.Emit and IL

Cons for rolling my own:

  • I'm not likely to get it right on the first (or even second) attempt.
  • After looking at NMock's source, it looks like routine stuff, the moment you get it.
  • It'll take quite a bit of time.

Looking at it this way, I think that it's no brainer what I'll use :-)

* This is acceptable under normal circumstances, but a bug** in the mocking library doesn't fall under the defination of nomral circumstances.

** Yes, it's a bug, a mock should be just like the object it replace.

More posts in "Rhino Mock" series:

  1. (30 Jun 2008) Getting closer to conclusion
  2. (29 Jun 2008) The role of Stub vs. Mock
  3. (29 Jun 2008) To be strict or not?