Friday, February 4, 2011

What is the best style/syntax to use with Rhino Mocks?

Multiple approaches exist to write your unit tests when using Rhino Mocks:

  • The Standard Syntax
  • Record/Replay Syntax
  • The Fluent Syntax

What is the ideal and most frictionless way?

  • Interesting question! My own preference is the for the reflection-based syntax (what I guess you mean by the Standard Syntax). I would argue that this is the most frictionless, as it does not add much extra code: you reference the stubs directly on your interfaces as though they were properly implemented.

    I do also quite like the Fluent syntax, although this is quite cumbersome. The Record/Replay syntax is as cumbersome as the Fluent syntax (if not more so, seemingly), but less intuitive (to me at least). I've only used NMock2, so the Record/Replay syntax is a bit alien to me, whilst the Fluent syntax is quite familiar.

    However, as this post suggests, if you prefer separating your expectations from your verifications/assertions, you should opt for the Fluent syntax. It's all a matter of style and personal preference, ultimately :-)

    From alastairs
  • Arrange, act, assert. Tho, I use MoQ and prefer the Arrange, Assert, Act, Verify. I like to set up everything before I act, rather than do the heavy lifting at the end.

    From Will
  • For .NET 2.0, I recommend the record/playback model. We like this because it separates clearly your expectations from your verifications.

    using(mocks.Record())
    {
        Expect.Call(foo.Bar());
    }
    using(mocks.Playback())
    {
        MakeItAllHappen();
    }
    

    If you're using .NET 3.5 and C# 3, then I'd recommend the fluent syntax.

0 comments:

Post a Comment