Mocking void method with Rhino mocks

Only a handful of people use unit test extensively. Out of those people only a handful believes in using Mocking framework. It's a debatable issue to use mocking framework in a unit test, which I'll not touch in this post. For those of you who just started in the software development field, I highly suggest to look into mocking framework in general especially when you're interested in doing Test Driven Development.

One thing that I haven't discovered until now is how to mock void methods with Rhino mocks. Putting something like

Expect.Call(someVoidMethod())

simply will not work as the expect.call expect a non void method. Ayende have a post that addresses this same issue here: Using Expect.Call(void method).

For me, I like using lambda expression the best. I think it's the cleanest/most readable technique:

Expect.Call( () => mock.someVoidMethodWithAParameter(someParamValue));

For methods that doesn't have any parameters, the following is acceptable:

Expect.Call( mock.someVoidMethodWithoutAnyParameter);

Notice that there isn't an empty bracket in the end. It's simply the method name.

Tested with
.Net 3.5
Rhino Mocks 3.5.0.1337
VS 2008
Resharper unit test runner

  1. says:

    great post, very usefull for me.. tnx!

  2. says:

    Very nice informations, thank sir..

  3. says:

    thanks for sharing your method, very helpful..

  4. Gest says:

    Really Helpful