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



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,



Comments

Silverligt Travel said:

Thanks. I think I had the same misunderstanding.

# October 22, 2008 12:56 AM

Bertkid said:

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.

# October 22, 2008 8:52 AM

Dew Drop - October 22, 2008 | Alvin Ashcraft's Morning Dew said:

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

# October 22, 2008 11:27 AM

Phil Leggetter said:

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?

# October 22, 2008 2:20 PM

Alex said:

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).

# October 22, 2008 2:55 PM

Mocks oder Subs said:

Pingback from  Mocks oder Subs

# October 23, 2008 12:41 AM

Christopher Bennage said:

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

# October 24, 2008 1:41 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