Hello World of Rhino Mocks

by percent20 1/16/2008 1:19:00 PM

Mock Objects are something that I hear more and more and more and more about as I continue my path into the TDD world, and even from some not in the TDD world.  They are a wonderful tool for helping you to create code for tests that hasn't been implemented yet. Or to help test your data access code without having to run to the database every time you run a test.  Before we go much further lets get a more "formal" definition of Mock Objects.  For that lets visit Wikipedia:

In object-oriented programming, mock objects are simulated objects that mimic the behavior of real objects in controlled ways. A computer programmer typically creates a mock object to test the behavior of some other object, in much the same way that a car designer uses a crash test dummy to test the behavior of a car during an accident.

To use Rhino Mocks first you need to download Rhino Mocks then unzip it somewhere on your hard drive.  Then in your projects just go to add references in the solution explorer then the browse tab then find the assembly where you unzipped it and tada you are read to start.

So let me introduce you to basically what the application is I wrote to mess with mock objects.

Getting Started

First, let me tell you I don't completely understand what is going on with Rhino Mocks and "how" it works.  At this point I know just enough to start "using" mock objects and that is what I want to pass on to you today.  I assembled this code together from around 20 different tutorials so please do not look at this as best practices.  So in typical TDD fashion lets start with test. (I am including the top portion of the file so you can see what I have above the actual test)

Test

HelloWorld Test

Firstly, don't forget to add references to NUnit and your Rhino Mocks assemblies.  You will also need to setup all the appropriate attributes for your tests.  If you do not know how to do this please visit my Part 2 of TDD for Beginners.

MockRepository mock = new MockRepository();

This is the object you create initially that will store all of your mock objects that you are going to be using. (That is what I gather from what it does and the name)

IWorld world = mock.CreateMock<IWorld>();

As best I understand this code we are creating an instance of IWorld object as if it was an object that inherited from IWorld.

using (mock.Record())

From my understanding this basically will run and in this block of code is where we load our mock object with what the objects will do.  Like we are going to basically say what the world object is going to do.

Expect.Call(world.GetWorld()).Return("World");

With this we just told our mock object that it can expect us to call the GetWorld method that is in the world object and it will return the string "World".  So now when we use this object later it will have a method implemented that will return a string.

using (mock.Playback())

In this section of code is where we start USING our mock objects to write our test.

Person p = new Person()
string saying = p.SayHello(world);

Here we create an instance of our person class that is going to call its method SayHello and pass it that mock world object we create above as if it a real and fully created world object.

Assert.AreEqual("Hello World", saying);

This is just a normal assertion that we do for our test.

Making the test Compile

At this point if you were to try and compile it wouldn't so we will implement the code to make it compile.  Lets start with the IWorld interface.

IWorld

That is all you need for the IWorld interface.  And that is all you will need TOTAL for the whole example for IWorld.  Next is the Person Class.

Person Class 

The thing to notice about this piece of code is that for our parameter we are using IWorld as the type and not World since we don't have a world object yet, but we know that we are going to implement it later.  Later the world object will inherit from the IWorld interface so the method will know what to do.

So now it should compile and the test should fail. This is good.

Making the test Pass

All that we need to do to make the test pass is to add change line of code now.

SayHello Method

With this code it takes the object that it was passed and uses it as if it where a regular a real object, but it was created as a mock object in our test.  The great thing is that since we created the mock object and gave it its mock functionality we can implement code to USE that functionality even though it really doesn't exist YET.

To drive home the point on my own and test further I went ahead and created a test using a real world object and a world object.  It worked like a charm.  Those are below.

World Object:

CropperCapture[31]

Real Object Test

World Object Test

Wrap-Up

I hoped that this helped to give you an understanding of mock objects and how to use Rhino Mocks to use mock objects.  I am starting to like mock objects the more I read about them mostly because I can not worry about the database code itself until last, as I don't enjoy it, and can start to work on the application itself and create a layer that is calling mock objects to retrieve data.  Then later turn those mock objects into a real data access layer and I am set to go.

Please feel free to leave any comments or criticisms the more the merrier.

RhinoMocks_1.zip (7.42 kb)

kick it on DotNetKicks.com

Currently rated 4.0 by 1 people

  • Currently 4/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Tags: , ,

.NET | TDD | Tutorials

Related posts

Comments

1/17/2008 11:18:20 AM

Steven Harman

I'm sure it was just a typo, but just to be clear
class World : IWorld
means that the class World implements IWorld, not inherits it. We know this because IWorld is an Interface, not a Class.

So, the following
IWorld world = mock.CreateMock<IWorld>();
returns an object that Implement the IWorld interface, and thus is able to act like an IWorld as long as you tell Rhino.Mocks to do so (in your mocks.Record() block).

Hope this helps!

Steven Harman us

1/17/2008 11:27:11 AM

Buddy Lindsey

Thank you for that. I am very much not used to interfaces still wasn't too sure of the terminology. Thanks for the clarification.

Buddy Lindsey

1/17/2008 11:20:16 PM

pingback

Pingback from lazycoder.com

links for 2008-01-18 | Lazycoder

lazycoder.com

1/18/2008 1:25:35 AM

pingback

Pingback from mindgravy.net

Mind Gravy » Blog Archive » links for 2008-01-18

mindgravy.net

1/18/2008 2:51:24 AM

pingback

Pingback from alvinashcraft.com

» Daily Bits - January 18, 2008 Alvin Ashcraft’s Daily Geek Bits: Daily links plus random ramblings about development, gadgets and raising rugrats.

alvinashcraft.com

1/18/2008 3:07:01 AM

pingback

Pingback from bradygaster.com

Excellent Noob RhinoMocks Post

bradygaster.com

1/19/2008 5:39:50 PM

Kamran

thanks for the intro article

Kamran us

1/19/2008 6:11:59 PM

percent20

@Kamran, I am glad I was able to help. Please stick around I might write other stuff which is helpful too.

percent20 us

1/20/2008 1:43:12 PM

pingback

Pingback from rtipton.wordpress.com

Weekly Link Post 25 « Rhonda Tipton’s WebLog

rtipton.wordpress.com

1/21/2008 9:45:33 AM

pingback

Pingback from code-inside.de

Wöchentliche Rundablage: .NET 3.5, WPF, LINQ, Tests, System.AddIn, SubSonic, Sandcastle | Code-Inside Blog

code-inside.de

1/22/2008 1:06:33 PM

pingback

Pingback from weblogs.asp.net

I have arrived - Beginnermediate ASP.NET

weblogs.asp.net

1/23/2008 10:23:39 PM

sundeep kamath

nice article for beginners!!!

sundeep kamath in

1/25/2008 5:09:14 AM

Leonardo Micheloni

Great article! thanks!

Leonardo Micheloni ar

2/27/2008 7:55:56 AM

Libor

thanks for your short intro, it helped me a lot (Im total noob in mocking Smile)

Libor cz

4/3/2008 11:37:30 AM

Buddy Stein

Hey we have the same name! Thanks for getting this running again.

Buddy Stein us

Add comment


(Will show your Gravatar icon)  

  Country flag

[b][/b] - [i][/i] - [u][/u]- [quote][/quote]



Live preview

7/25/2008 1:11:09 AM

Powered by BlogEngine.NET 1.3.0.0
Theme by Mads Kristensen


My Flare

AddThis Feed Button

National Blog Posting Month

Eagle Scout

I'm Test Driven

[Reserved for MVP status I want to earn]

View Buddy Lindsey's profile on LinkedIn

Twitter



Disclaimer

The opinions expressed herein are my own personal opinions and do not represent my employer's view in anyway.

© Copyright 2008

Sign in