Devlico.Us
CodeBetter.Com
RSS 2.0 via Feedburner
           Do you Twitter? Follow us @devlicious

Derik Whittaker

Thoughts on Software Development, .Net, OOP, Design Patterns and all things cool



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.



Comments

damieng said:

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

# June 13, 2007 10:55 AM

Derik Whittaker said:

@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'.

# June 13, 2007 11:06 AM

Louis Haskett said:

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!

# June 13, 2007 3:50 PM

Derik Whittaker said:

@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

# June 13, 2007 4:20 PM

Aashish said:

Good stuff

# September 12, 2008 1:09 AM

Aashish Sethi said:

Good stuff

# September 12, 2008 1:10 AM

zhaorui said:

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

# November 16, 2008 12:25 PM

Leave a Comment

(required)  
(optional)
(required)  

Enter the numbers above:
Add

About Derik Whittaker

Derik is a .Net Developer/Architect specializing in WinForms working out the northern suburbs of Chicago. He is also believer and advocate for Agile development including SCRUM, TDD, CI, etc.

When Derik is not writing code he can be found spending time with his wife and young son, climbing on his bouldering wall, watching sports (mostly baseball), and generally vegging out. Check out Devlicio.us!

Our Sponsors

Proudly Partnered With


This Blog

Syndication

News