Mocks and Stubs - The difference is in the flow of information - ISerializable - Roy Osherove's Blog

Mocks and Stubs - The difference is in the flow of information

In class today when explaining the difference between stubs and mocks, I realized a simple explanation to this:

Stubs and mocks may seem the same but the flow of information from each is very different.

  • Stubs provide input for the application under test so that the test can be performed on something else.
  • Mocks provide input to the test to decide on pass\fail. the opposite direction.

The following diagram shows the flow of critical information:

image

The red arrows signify the "critical" flow of information that is used to decide if the test had passed. A stub can never fail a test, so there is no flow of information back to the test. the mock records things and then tells our test if it's expectations have been met according to its recording.

A stub is application facing, and a mock is test facing. Here's a more in depth overview of the two, but I haven't seen this explained as I just did anywhere. It's important to know and distinguish the two since many frameworks for mocking use these terms for different kinds of objects.

Published Sunday, September 16, 2007 5:26 PM by RoyOsherove

Comments

Wednesday, September 19, 2007 9:50 AM by DotNetKicks.com

# Mocks and Stubs - The difference is in the flow of information

You've been kicked (a good thing) - Trackback from DotNetKicks.com

Thursday, September 20, 2007 3:16 PM by tobsen

# re: Mocks and Stubs - The difference is in the flow of information

Very well explained and after I read Fowler's comparison of Mocks, Fakes and Stubs I am now fully aware of what's the difference between mocks and stubs. However I have to admit that I used Rhino.Mock's mocks also as stubs. That will change from now on I think.

+1 kick from me.

Saturday, November 17, 2007 4:47 PM by ISerializable - Roy Osherove's Blog

# Mocking LINQ Queries, Extension methods and Anonymous Types

One of the things I wanted to show at my Interactive session on unit testing tips and tricks at TechEd

Saturday, July 12, 2008 6:23 AM by ISerializable - Roy Osherove's Blog

# Over Specification in Tests

I've gotten the chance to visit and consult various companies and I see this all the time: Over specification

Saturday, July 12, 2008 10:34 AM by Paul Mrozowski's Blog - What is Dependency Injection?

# Paul Mrozowski's Blog - What is Dependency Injection?

Pingback from  Paul Mrozowski's Blog - What is Dependency Injection?