Difference between Mocks and Stubs.

There are nowadays two terms that are used by the developers : 'stub' and 'mock'. As I found in  

the article of Martin Fowler 'Mocks Aren't Stubs', he is using the vocabulary of Gerard

Meszaros's book, where there is the division into : 'dummy', 'fake', 'stub' and 'mock' points.

I'll metnion only what 'dummy' and 'fake' are, and I'll try to concentrate over 'mock' and

'stub'. So 'dummy' objects as Thesaurus says represents a copy, figure, form, model in its mean.

In reallity passing to developper language, the goal of dummy objects is to be passed, in this

sence the general use of dummy objects is to fill parameter lists. I should metion that 'dummy'

objects do not have their working implementation. If we are speaking about 'fake' objects, than

from the beginning we must emphasize that these type objects have their implementations, an

important point of distinguishing here is that 'fake' objects are not used for production, they

represent something like an alternate route, timesaving method. For example an SQLLite database

represents a 'fake' object.
In the following part of this article I'll try to concentrate on two important concepts: 'stub'

and 'mock'. Many often confuse these terms speaking about them as about the same thing. From the

conceptual point of view they both represent methodologies that share some common things. I

thinks there is also an historical motivation of the confusion between them: from the first

appeared the therm 'stub' and it was used for a long period of time, when in some communities

appeared and started to be used the them of 'mock'. On the first glance they are suitable to

accomplish the same things. I can caracterize a 'mock' as an imitation or make-believe, and a

'stub' as a dock. The 'mock' object request the behavior check, while 'stub' is for state check.

The 'mock' represents an object that englobes a specification of expectations for the calls it is

expected to receive, while 'stub' represents preserved answers to requests that are made during

the test ( as a rule, the 'stub' does not respond to smth else that's programmed especially for

the test).


We'll examine an example of using 'mock' and 'stub' in the same scenario and we'll get deeper

into discussion based on some code. So we have the situation that based on the number of users on

a portal, the owner of the portal receives an sms on his mobile phone to be informed about the

event. Let's assume that at 100.000 visitors he gets a message about this. We'll use Rhino Mocks

and NUnit for our testing purposes.

Class Msg - a simple CLR object that encapsulates the message to be send via SMS:

 public class Msg
 {
  private string message;
  public Msg(string message)
  {
   this.message = message;
  }

  public string Message
  {
   get
   {
    return message;
   }
   set
   {
    message = value;
   }
  }
 }

Interface IServiceSMS contains the method Send() for sending the SMS:

 using TheDiff.BusinessObjects;

 namespace TheDiff.Interfaces
 {
     public interface IServiceSMS
     {
         bool Send(Msg msg);
     }
 }

Class for tests MockTests:

using Rhino.Mocks;
using NUnit.Framework;
using TheDiff.BusinessObjects;
using TheDiff.Interfaces;

namespace TheDiff
{
    [TestFixture]
    public class MockTests
    {
        private IServiceSMS serviceSMS;
        [SetUp]
        public void SetUp()
        {
            serviceSMS = MockRepository.GenerateMock<IServiceSMS>();
        }

        [Test]
        public void Mocker()
        {
            var msg = new Msg("Online users are 100.000!");
            serviceSMS.Expect(x => x.Send(msg)).Return(true);
            var wasSent = serviceSMS.Send(msg);
            Assert.IsTrue(wasSent);
            serviceSMS.VerifyAllExpectations();
        }

    }
}

Class for tests StubTests:

using Rhino.Mocks;
using NUnit.Framework;
using TheDiff.BusinessObjects;
using TheDiff.Interfaces;

namespace TheDiff
{
    [TestFixture]
    public class StubTests
    {
        private MockRepository mocks;
        private IServiceSMS serviceSMS;
        [SetUp]
        public void SetUp()
        {
            mocks = new MockRepository();
            serviceSMS = mocks.Stub<IServiceSMS>();
        }

        [Test]
        public void Stuber()
        {
            var msg = new Msg("Online users are 100.000!");

            using (mocks.Record())
            {
                SetupResult.For(serviceSMS.Send(msg)).Return(true);
            }
            var wasSent = serviceSMS.Send(msg);
            Assert.IsTrue(wasSent);
        }

    }
}

 

 

  • Share This Post:
  • Share on Twitter
  • Share on Facebook
  • Share on Technorati

Print | posted @ Tuesday, April 21, 2009 12:29 AM

Comments on this entry:

Gravatar # re: Difference between Mocks and Stubs.
by Alex at 4/21/2009 7:35 PM

Thank you for your post, it indeed helped me in understanding the differenece between these two important concepts. Keep it going!
Post A Comment
Title:
Name:
Email:
Website:
Comment:
Verification: