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
Rhino Mocks vs NMock: Part 1, Stubs

Over the past few months I have really started to believe in the use of Mocks.  When I first started using mocks, I started with NMock (mostly because that is what we had at work).  Shortly after that I started playing around with Rhino Mocks at home. 

Since I have been playing around with both frameworks, I thought I would create a series of post that compare and contrast the two.  This initial post is going to be showing the differences in how to create and use stub objects.

Stubs are as defined my Fowler as :

Stubs provide canned answers to calls made during the test, usually not responding at all to anything outside what’s programmed in for the test. Stubs may also record information about calls, such as an email gateway stub that remembers the messages it ’sent’, or maybe only how many messages it ’sent’.

Creating stubs with Rhino Mocks:

Notice here that the creation of a mock involves only 2 lines of code.  Also notice how you don't have to use 'magic strings' to define a property, you simply populate the method with your required value.

Creating stubs with NMock

Notice here that the creation of the same mock requires 3 lines, 1 more than Rhino Mocks.  This to me is not that big of a deal.  What does stand out to me is how NMock has the developer set a mock property value.  Unlike Rhino Mocks where you simple set the property like you would in code, you have to traverses their framework to get the same result.  The biggest difference however is the use of 'magic strings' when defining what property to use with the stub.

 

Analysis
After looking at the 2 different ways to create mocks did you notice how they differed?  Rhino Mocks was built with Refactoring and TDD in mind.  You will notice that Rhino Mocks creation is shorter, simpler and less error prone.  The reason Rhino Mocks is less error prone is because of the lack of 'magic strings'.  When a framework like these require the developer to hardcode property names or method names these CANNOT be found during refactoring.  The only way to know something changed is by running the test.  I would rather have my compiler tell me something was wrong.

Personally, I also like how Rhino Mocks is less code, Having to remember how to populate a stub is the last thing I want to have to do.  NMock is simply too verbose in this situation.

Winner of Round 1:
Rhino Mocks

Next Time:
Next time I will show how the two different frameworks handle expectations on method calls.


Posted 06-13-2007 8:49 AM by Derik Whittaker
Filed under: , , , ,

[Advertisement]

Comments

damieng wrote re: Rhino Mocks vs NMock: Part 1, Stubs
on 06-13-2007 10:55 AM

While I agree that setting a property in NMocks is rather ugly saying that it takes 3 lines over Rhino's 2 is a little unfair when you could simply write it as:

// Create the stub instance

ITask stubTask = new Mockery().NewMock < ITask >();  

// Populate it with your mock value

Stub.On( stubTask ).GetProperty( "TaskSubject" ).Will( Return.Value( "Some Value here" ) );

[)amien

Derik Whittaker wrote re: Rhino Mocks vs NMock: Part 1, Stubs
on 06-13-2007 11:06 AM

@Damieng

ok, so they both can be done in 2 lines in this case.

The actual point was how with Rhino Mocks you use less code, and you dont need the 'magic string'.

Louis Haskett wrote re: Rhino Mocks vs NMock: Part 1, Stubs
on 06-13-2007 3:50 PM

I gotta agree with derik on this one:

Magic strings == BadStuff (TM).

But doesn't Rhino Mock require some sort of "its ready to use now" call after your setup your mock?

I like that Rhino Mock is less code.  Though I do think that the way the nMock code reads is cool, it reads a lot like english!

Derik Whittaker wrote re: Rhino Mocks vs NMock: Part 1, Stubs
on 06-13-2007 4:20 PM

@Lou,

Rhino Mocks does not require anything for only using stubs.  It is when you want to have methods return stubs/mocks does it required you to make the call.

The 'call' you are looking for is

mocks.ReplayAll();

BTW, you and your 'reads like english'.

Derik

Aashish wrote re: Rhino Mocks vs NMock: Part 1, Stubs
on 09-12-2008 1:09 AM

Good stuff

Aashish Sethi wrote re: Rhino Mocks vs NMock: Part 1, Stubs
on 09-12-2008 1:10 AM

Good stuff

zhaorui wrote weekly review 200845: Work Hard Not Enough
on 11-16-2008 12:25 PM

这一周主要是在NCC那边看DGEL那本书,虽然看的不是特别多,但是DAAB的部分还是蛮有收获;但是HQIS的项目本身没有什么进展,下周要开始写文档。CMI的程序进展还是不够快,后面几周要集中注意力,减...

Ryan Pedersen wrote re: Rhino Mocks vs NMock: Part 1, Stubs
on 02-17-2009 9:59 AM

Did you ever get around to posting Round 2?

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)