Tags: , , , | Categories: .NET, Architecture Posted by oleksii on 2/1/2011 6:01 PM | Comments (0)

I got an interesting question today and I partly failed to answer it. 

The question was: what is the difference between a mock and a stub? 

Although I spent sufficient amount of time writing unit tests with and without mocking objects, I was unable to provide a clear answer. Having searched the internet afterwards, I found one of the classic article by M. Fowler that I managed to miss somehow. The article is very detailed, but long story told short:

Using stubs – is the classic TDD approach where unit tests are designed to check state. A general core workflow would be calling a method of an object/type with subsequent verification that the state has been updated.

Using mocks – is a TDD variation - BDD - that aims to track behaviour. Typical scenario would be creating a pre-programmed workflow and check if a real-world object follows the predefined behaviour.

What is better? Which path to choose? These are debatable questions and are highlighted by Martin in his article “Mocks Aren't Stubs”.

If you enjoyed this post, make sure you subscribe to my RSS feed!

blog comments powered by Disqus