Home > General .NET Coding > Rhino Mocks v3.2 – New Syntax

Rhino Mocks v3.2 – New Syntax

I have been using Rhino Mocks v3.2 with my current project and i’m finding it is a test mocking tool I can’t live without!

What is Rhino Mocks?

"Rhino.Mocks is an attempt to create easier way to build and use mock objects and allow better refactoring support from the current tools. It’s a hybrid approach between the pure Record/Replay of EasyMock.Net’s model and NMock’s expectation based model. Rhino.Mocks originated from EasyMock.Net and attempt to improve on their model to create easy to use and power mocking framework. It’s free for use and modification for open source and commercial software.

Licensing: Rhino Mocks is Free Software which is released under the BSD license."

(http://ayende.com/projects/rhino-mocks.aspx )

One aspect I like (which is mentioned on the Rhino homepage) is the fact that Rhino uses typed objects not strings like the others i.e. NMock, TypeMock.Net. Having the objects typed means that the build/compiler process will catch any human error mistakes. If they are not typed i.e. in strings like "ICustomerView" then if they are spelt incorrect or used incorrectly then they will only "blow up" or throw exceptions on you at runtime, which wastes time – that’s not really nice! Hence why I like Rhino.

This post will demonstrate the new Rhino Mocking syntax in v3.2 which I think is getting really close the "English spoken behavior" for tests.  What I mean by this is that I would like to see a block which illustrates my Expectations and another block which illustrates my Assertions allowing the Unit Test(s) to be easy read and understood.

There is a blog post at  e-TOBI.net which illustrates really nicely the different versions of usage for Rhino Mocking. 

Below is a few examples from this site demonstrating the older syntax versions:

Example using "Replay All" (Expectations) and "Verify All" (Assertions/Verifications)

           

        MockRepository mocks = new MockRepository();

        IDependency dependency = mocks.CreateMock<IDependency>();

 

        // define expectations

        Expect.Call(dependency.GetSomething("parameter")).Return("result");

        dependency.DoSomething();

        mocks.ReplayAll();

 

        // run test subject

        Subject subject = new Subject(dependency);

        subject.DoWork();

 

        // verify expectations

        mocks.VerifyAll();

Example using "Record" (Expectations) and "Playback" (Assertions/Verifications)

         
        MockRepository mocks = new MockRepository();

        IDependency dependency = mocks.CreateMock<IDependency>();

 

        using (mocks.Record())

        {

             Expect.Call(dependency.GetSomething("parameter")).Return("result");

             dependency.DoSomething();

        }

 

        using (mocks.Playback())

        {

             Subject subject = new Subject(dependency);

             subject.DoWork();

        }

So when I analyse the above two versions I am left feeling confused by the syntax by what’s actually happening. I have used both and they work just fine but I would rather have something that is more English readable or "English Fluent" as Rhino Mocks Website states.

This problem then leads developers spending hours to understand how this all works and where should I put my Expectations and Assertions. A prime example is when I try and teach these two syntax versions above to other developers. I can understand why they don’t pick it up on the first go! So the latest version is getting better and is more "English Fluent" but does introduce delegates which then may also confuse developers which haven’t used them before.

The "blocks which illustrates my Expectations and another block which illustrates my Assertions" which I spoke about earlier are basically defined now with the words of "Expecting" and "Verify" – so we are nearly there! 

Example using the new syntax in v3.2

 

        MockRepository mocks = new MockRepository();

        IDependency dependency = mocks.CreateMock<IDependency>();

 

        With.Mocks(mocks).Expecting(delegate

        {

             Expect.Call(dependency.GetSomething("parameter")).Return("result");

             dependency.DoSomething();

        })

        .Verify(delegate

        {

             Subject subject = new Subject(dependency);

             subject.DoWork();

        });

Below is a couple of Unit Test examples using the syntax from v3.2:

       

        [Test]

        public void SendRemoteIPCallsSendRemoteIPFromService()

        {

            _remoteIPPresenter = new RemoteIPPresenter(_remoteIPView, _remoteIPService);

 

            With.Mocks(_mockRepository).Expecting

                (delegate

            {

                _remoteIPService.SendRemoteIP(false);

            }

            )

            .Verify

            (delegate

            {

                _remoteIPPresenter.SendRemoteIP();

            }

            );

        }

        [Test]

        public void RetrieveCurrentRemoteIPCallsRetrieveRemoteIPFromLocalAndDisplayRemoteIP()

        {

            _remoteIPPresenter = new RemoteIPPresenter(_remoteIPView, _remoteIPService);

  

            With.Mocks(_mockRepository).Expecting

                (delegate

                {

                    RemoteIP remoteIP = new RemoteIP("125.13.23.3", DateTime.Now);

                    Expect.Call(_remoteIPService.RetrieveRemoteIPFromLocal()).Return(remoteIP);

                    _remoteIPView.DisplayRemoteIP(remoteIP.IPAddress, remoteIP.DateReceived);

                }

            )

            .Verify

            (delegate

            {

                _remoteIPPresenter.RetrieveCurrentRemoteIP();

            }

            );

 

        }

So I suggest you start using the syntax from v3.2.  Rhino is certainly a mocking framework for .NET I would recommend to any developer.

Happy coding!

Yellow Duck Guy
Greg Olsen.

Advertisements
Categories: General .NET Coding
  1. Unknown
    October 8, 2007 at 8:19 am

    Ewwww delegates. I think I prefer the record and playback option 🙂

  2. LOVE_MOSS_NOT
    November 23, 2010 at 2:04 pm

    yeah bro, the using looks better… that delegate looks fugly

  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: