What are the differences between mocks and stubs

  • Dummy objects are passed around but never actually used. Usually they are just used to fill parameter lists.
  • Fake objects actually have working implementations, but usually take some shortcut which makes them not suitable for production (an in memory database is a good example).
  • 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'.
  • Mocks are what we are talking about here: objects pre-programmed with expectations which form a specification of the calls they are expected to receive.
http://martinfowler.com/articles/mocksArentStubs.html
Some authors[1] draw a distinction between fake and mock objects. Fakes are the simpler of the two, simply implementing the same interface as the object that they represent and returning pre-arranged responses. Thus a fake object merely provides a set of method stubs.
In the book "The Art of Unit Testing"[2] mocks are described as a fake object that helps decide whether a test failed or passed by verifying whether an interaction with an object occurred. Everything else is defined as a stub. In that book, "Fakes" are anything that is not real. Based on their usage, they are either stubs or mocks.
Mock objects in this sense do a little more: their method implementations contain assertions of their own. This means that a true mock, in this sense, will examine the context of each call— perhaps checking the order in which its methods are called, perhaps performing tests on the data passed into the method calls as arguments.
http://en.wikipedia.org/wiki/Mock_object#Mocks.2C_fakes_and_stubs

To put it in a single sentence, mocks are about behavior and stubs are about state. What the hell does that mean you say!? 
A unit test that uses a stub is interested in being able to make a call to an object and getting data back that will allow the method under test the ability to continue with a given set of values. Those values are designed to flex the method’s abilities and see if it succeeds in all the proper test scenarios. So, stubs provide state (data) back to the method under test and your unit test has all the information it needs within itself to determine success or failure. 
Mocks on the other hand are about behavior. Under the current conditions, did my method pass the right values? Did it make all of the right calls and ONLY the calls that were necessary?
http://marknic.net/2010/01/11/mocks-vs-stubs/


0 komentarze:

Prześlij komentarz

 
Tomasz Kulig