Derik Whittaker

Syndication

News


Images in this post missing? We recently lost them in a site migration. We're working to restore these as you read this. Should you need an image in an emergency, please contact us at imagehelp@codebetter.com
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,

 


    Posted 10-22-2007 7:35 AM by Derik Whittaker
    Filed under: , ,

    [Advertisement]

    Comments

    Jim Bolla wrote re: Mocking Voids with LastCall and RhinoMocks
    on 10-22-2007 10:22 AM
    Christopher Bennage wrote re: Mocking Voids with LastCall and RhinoMocks
    on 10-22-2007 1:46 PM

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

    Derik Whittaker wrote re: Mocking Voids with LastCall and RhinoMocks
    on 10-22-2007 1:59 PM

    @Jim,

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

    Derik

    DotNetKicks.com wrote Mocking Voids with LastCall and RhinoMocks
    on 10-25-2007 9:46 AM

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

    Pages tagged "mocking" wrote Pages tagged "mocking"
    on 01-16-2008 1:11 AM

    Pingback from  Pages tagged "mocking"

    About The CodeBetter.Com Blog Network
    CodeBetter.Com FAQ

    Our Mission

    Advertisers should contact Brendan

    Subscribe
    Google Reader or Homepage

    del.icio.us CodeBetter.com Latest Items
    Add to My Yahoo!
    Subscribe with Bloglines
    Subscribe in NewsGator Online
    Subscribe with myFeedster
    Add to My AOL
    Furl CodeBetter.com Latest Items
    Subscribe in Rojo

    Member Projects
    DimeCasts.Net - Derik Whittaker

    Friends of Devlicio.us
    Red-Gate Tools For SQL and .NET

    NDepend

    SlickEdit
     
    SmartInspect .NET Logging
    NGEDIT: ViEmu and Codekana
    LiteAccounting.Com
    DevExpress
    Fixx
    NHibernate Profiler
    Unfuddle
    Balsamiq Mockups
    Scrumy
    JetBrains - ReSharper
    Umbraco
    NServiceBus
    RavenDb
    Web Sequence Diagrams
    Ducksboard<-- NEW Friend!

     



    Site Copyright © 2007 CodeBetter.Com
    Content Copyright Individual Bloggers

     

    Community Server (Commercial Edition)