views:

288

answers:

1

I notice that if I write

Expect.Call(delegate { obj.Method(null); }).IgnoreArguments().Do(
  new Action(() => {Console.Write("executed");throw new Exception(); }));

Provided that the method does run, MbUnit will recieve the "executed" message but will not detect an exception being thrown. Does anyone know why that is so? Is this something about the way anonymous methods are treated by the framework or about the way Rhino Mocks executes them?

+1  A: 

Lamba's are executed in the scope they're written in. If you declare an integer before the Expect.Call you can access it inside the lambda.

The exception has nothing to do with scope though. I guess it get's caught somewhere. This could be by rhino-mocks or by the code that calls obj.Method(). I'll have to try that.

edit Tried it with this code:

        try
        {
            int i = 0;

            TestClass mock = MockRepository.GenerateMock<TestClass>();
            mock.Expect(x => x.Method()).Do(new Action(() => { i++; throw new Exception(); }));

            mock.Method();
        }
        catch
        {
            Console.WriteLine("Bla!");
        }

Where testclass.Method is just an empty virtual method. The exception gets thrown here. So rhino-mocks doesn't catch the exception. My guess is that the code you're testing that isn't shown in your example does the catching. Not obj.Method but the code that calls obj.Method.

I hope this makes sense because my head is spinning.

Mendelt
it is not caught by obj.Method() if it is caught by Rhino mocks that is indeed interesting...I wonder why Ayende did that
George Mauer
Hmm...thanks a lot, I should have probably tried my simplification myself first. Strange though, I can't recall having written any try..catch blocks on this project yet.
George Mauer
I didn't mean obj.Method catches the exception but the code that calls obj.Method. The call isn't shown in your example but I'm guessing you don't call obj.Method directly in your test, that wouldn't test much. :-)
Mendelt