views:

78

answers:

2

A citation from "The art of unit testing" book:

In Rhino Mocks, strict mocks are created by calling the StrictMock method. Unexpected method call exceptions will always be thrown, even if your test contains a global try-catch clause, which you’d think would catch such an exception thrown from the isolation framework.

So how exactly can I implement this behaviour in the program of my own?

A: 

One way might be to implement an interception framework, such as Unity.Interception (others also exists, e.g. from Castle).

Using Unity.Interception you can write an implementation of ICallHandler, which has an Invoke method where you can trap and re-throw exceptions:

public IMethodReturn Invoke(IMethodInvocation input, GetNextHandlerDelegate getNext)
{
    var methodReturn = getNext().Invoke(input, getNext);

    // did the method raise an exception?
    if (methodReturn.Exception != null)
    {
        // handle it... re-throw it if necessary...
    }
}

Of course delving into the source code of Rhino.Mocks would be the most direct way to do this. At the very least, use Reflector and browse the dll.

code4life
+1  A: 

You should never really have this type of scenario in your application as you shouldn't be catching global exceptions. You should only catch the type of exceptions you are going to handle, everything else should be allowed to bubble up.

James