Hello World of Rhino Mocks

by percent20 16. January 2008 13:19

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

Tags: , ,

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 United States

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 United States

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 United States

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 India

1/25/2008 5:09:14 AM #

Leonardo Micheloni

Great article! thanks!

Leonardo Micheloni Argentina

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 Czech Republic

4/3/2008 11:37:30 AM #

Buddy Stein

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

Buddy Stein United States

9/7/2008 4:16:42 AM #

kabonfootprint

thanks for your short intro. thanks

kabonfootprint United States

9/7/2008 12:55:51 PM #

Cyron

Please help me to understand this. Mocks are to be substitutes for real objects, aren't they? They are generated so you can write and run tests without the need for the production code. Now what I see in your sample is that your Mocks reference real objects. That kind of confuses me. I could just refer the real objects directly without your mocks at all and achieving the same results.

Cyron Germany

9/8/2008 1:01:46 AM #

Cyron

Nevermind, I got it now. Smile

Cyron Germany

11/13/2008 4:16:22 AM #

Alec He

As I know, the Rhino Mock has updated to version 3.5.
The CreateMock<T>() is replace with StrickMock<T>(), and add DynamicMock and PartialMock.

Alec He People's Republic of China

12/9/2008 4:42:35 AM #

Flominator

The example with Expect.Call only works for methods, that are not void. Using it for void methods results in an error like "argument type 'void' is not assignable". To avoid this, use LastCall.IgnoreArguments(); instead.

Flominator Germany

12/25/2008 8:28:28 AM #

Pankaj

Nice Article..Good work...Thanks!!!

Pankaj United States

1/12/2009 5:30:16 AM #

DotNetGuts

Article was simple and helpful to start.  Could you please explain more with any of real world example for better understanding.

Thanks,
DotNetGuts
http://dotnetguts.blogspot.com

DotNetGuts United States

5/29/2009 4:47:29 PM #

livechat operators

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 simulate the dynamic behavior of a human in vehicle impacts.
  

livechat operators United States

6/9/2009 3:31:34 AM #

Tukang Nggame

Thank you for another great article. Where else could anyone get that kind of information

Tukang Nggame United States

6/21/2009 9:24:27 PM #

Butuan City

thanks for the information.. keep up the good works...

Butuan City United States

Add comment


(Will show your Gravatar icon)

  Country flag

biuquote
  • Comment
  • Preview
Loading



Powered by BlogEngine.NET 1.5.0.7
Theme by Mads Kristensen

About the author

Something about the author