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
Mocks vs. Stubs and my 'Ah ha' moment

Last night on twitter Aaron Jenson made the statement

'moq's expect method needs to be renamed'

'setting the expectation is not what you should be doing. you don't *care* if it's called.'

I then started a short conversation with Aaron about this.  I have been using RhinoMocks for 2+ years and never really gave a second thought to the 'real' difference between a Mock and a Stub. 

I have mostly used Mocks with the Expect syntax and rarely ever used Stubs and their syntax.  But after re-reading the Rhino 3.5 WIKI I now have an understanding of what Aaron was referring to and why the point he made me finally go 'Ah ha' I have been thinking about it in the right way, but implementing it in the wrong fashion.

What is a Mock?
A mock is an object that we can set expectations on, and which will verify that the expected actions have indeed occurred.

In English...This is a method call that must take place, but you do not directly care about its results.

What is a Stub?
A stub is an object that you use in order to pass to the code under test. You can setup expectations on it, so it would act in certain ways, but those expectations will never be verified.

In English...This is a method call that must take place, and you DO care about its results because these results will be used in your class under test.


If you start to think about Mocks/Stubs in this light it does make sense.  In fact it even starts to clear things up (assuming you were still a bit foggy on the subject).

I think that if authors of the various mocking frameworks really want to make sure people use the correct terminology and syntax, they should limit the framework to ONLY work as the intent specifies.

Till next time,


Posted 10-21-2008 7:20 PM by Derik Whittaker
Filed under: ,

[Advertisement]

Comments

Adam wrote re: Mocks vs. Stubs and my 'Ah ha' moment
on 10-21-2008 9:36 PM

I believe you mixed up your 'In English' translations of what a mock and a stub is.

Silverligt Travel wrote re: Mocks vs. Stubs and my 'Ah ha' moment
on 10-22-2008 12:56 AM

Thanks. I think I had the same misunderstanding.

Bertkid wrote re: Mocks vs. Stubs and my 'Ah ha' moment
on 10-22-2008 8:52 AM

This sounds absolutely backwards to me.

"Stub" originally (still does?) means an empty method that has not yet been implemented.  To me, that matches what you wrote above as Mock.

Dew Drop - October 22, 2008 | Alvin Ashcraft's Morning Dew wrote Dew Drop - October 22, 2008 | Alvin Ashcraft's Morning Dew
on 10-22-2008 11:27 AM

Pingback from  Dew Drop - October 22, 2008 | Alvin Ashcraft's Morning Dew

Phil Leggetter wrote re: Mocks vs. Stubs and my 'Ah ha' moment
on 10-22-2008 2:20 PM

Is this terminology specific to RhinoMocks? I use Mock and the meaning are reversed.In NMock you create a mock of an interface and stub out any methods that may or may not get called and you don't really care if they get called since it's not what you are testing. If you don't stub out a method and it's called then something has gone wrong. The main point is that you set expectations on the methods of the mocked interface to ensure that they are called in the way you expect.

Basically I agree with Bertkid. Can anybody 100% clarify?

Alex wrote re: Mocks vs. Stubs and my 'Ah ha' moment
on 10-22-2008 2:55 PM

Even reversed, the English descriptions are incorrect.  The difference is in verification, not in results.

See xunitpatterns.com/Test%20Double.html for more information.  (Note that Martin Fowler revised his _Mocks Aren't Stubs' article to use this terminology).

Mocks oder Subs wrote Mocks oder Subs
on 10-23-2008 12:41 AM

Pingback from  Mocks oder Subs

Christopher Bennage wrote re: Mocks vs. Stubs and my 'Ah ha' moment
on 10-24-2008 1:41 PM

I had the same epiphany with 3.5 as well. Funny how that happens.

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)