Devlico.Us
CodeBetter.Com
RSS 2.0 via Feedburner
           Do you Twitter? Follow us @devlicious

Derik Whittaker

Thoughts on Software Development, .Net, OOP, Design Patterns and all things cool



Mocking Voids with LastCall and RhinoMocks

When following TDD, using mocking tools like RhinoMocks (here) or NMock (here) is a critical part of the process.  By utilizing mocking tools you can better isolate your tests to only test the code that you have direct control over.  This can prove to be critical component to building out your test suite.

One thing that I have found fairly poorly documented about mocking is how to mock void methods.  Today I thought I would give a quick how-to on how to mock voids.  My mocking tool of choice is RhinoMocks, so the code below will be specific to that mocking framework.

Mocking voids is really not that much different then mocking methods with return values.  Except for one slight thing, you actually have to make a call to your method on your mock, then setup the expectation on that call.  Below I will put the code to mock non-void methods, then to mock void methods just to show the differences.

Mocking non-void methods

  1. MockRepository mocker = new MockRepository();
  2. ISomeObject someObjectMock = mocker.CreateMock<ISomeObject>();
  3. Expect.On( someObjectMock).Call( request.IsRequestValid() ).Return( true );
  4. mocker.ReplayAll();
  5. .... Make some call that will exercise your mocks
  6. mocker.VerifyAll();

Mocking void methods

  1. MockRepository mocker = new MockRepository();
  2. ISomeObject someObjectMock = mocker.CreateMock<ISomeObject>();
  3. someObjectMock.SomeVoidMethod(0, "", "" );
  4. LastCall.IgnoreArguments();
  5. mocker.ReplayAll();
  6. .... Make some call that will exercise your mocks
  7. mocker.VerifyAll();

You will notice that the two different methods are pretty much the same EXCEPT for lines 3 & 4 in the void methods.  When mocking void method you need to actually make the call on the mock object for the method you expect to call later in your test.  This will setup the call inside the mocking framework.  The next step is to use the 'LastCall' (some info here) object.  The LastCall object is used ONLY for mocking void methods.  Unlike the 'Expect' object you do NOT need to provide the object that the void method is on, you only need to setup the expectations for the call.  This includes things like the number of calls or the types of arguments for the call.

Mocking voids is no more complicated then non-voids, but it does require a little more work and the use of different objects.

I hope this helps.

Till next time,

 



    Comments

    Christopher Bennage said:

    @Jim- woo hoo! thanks for making my day!  (This is what I get for not reading my feeds for a week.)

    # October 22, 2007 1:46 PM

    Derik Whittaker said:

    @Jim,

    I saw your post after i created mine.  I guess this post is still useful on older versions of RhinoMocks.

    Derik

    # October 22, 2007 1:59 PM

    DotNetKicks.com said:

    You've been kicked (a good thing) - Trackback from DotNetKicks.com

    # October 25, 2007 9:46 AM

    Pages tagged "mocking" said:

    Pingback from  Pages tagged "mocking"

    # January 16, 2008 1:11 AM

    Leave a Comment

    (required)  
    (optional)
    (required)  

    Enter the numbers above:
    Add

    About Derik Whittaker

    Derik is a .Net Developer/Architect specializing in WinForms working out the northern suburbs of Chicago. He is also believer and advocate for Agile development including SCRUM, TDD, CI, etc.

    When Derik is not writing code he can be found spending time with his wife and young son, climbing on his bouldering wall, watching sports (mostly baseball), and generally vegging out. Check out Devlicio.us!

    Our Sponsors

    Proudly Partnered With


    This Blog

    Syndication

    News