Understanding stubs, fakes and mocks.

aleczandru picture aleczandru · Feb 15, 2013 · Viewed 8.5k times · Source

I have just started to read Professional Test Driven Development with C#: Developing Real World Applications with TDD

I have a hard time understanding stubs, fakes and mocks. From what I understand so far, they are fake objects used for the purpose of unit testing your projects, and that a mock is a stub with conditional logic into it.

Another thing I think I have picked up is that mocks are somehow related with dependency injection, a concept which I only managed to understand yesterday.

What I do not get is why I would actually use them. I cannot seem to find any concrete examples online that explains them properly.

Can anyone please explain to me this concepts?

Answer

Jason Evans picture Jason Evans · Feb 15, 2013

As I've read in the past, here's what I believe each term stands for

Stub

Here you are stubbing the result of a method to a known value, just to let the code run without issues. For example, lets say you had the following:

public int CalculateDiskSize(string networkShareName)
{
    // This method does things on a network drive.
}

You don't care what the return value of this method is, it's not relevant. Plus it could cause an exception when executed if the network drive is not available. So you stub the result in order to avoid potential execution issues with the method.

So you end up doing something like:

sut.WhenCalled(() => sut.CalculateDiskSize()).Returns(10);

Fake

With a fake you are returning fake data, or creating a fake instance of an object. A classic example are repository classes. Take this method:

public int CalculateTotalSalary(IList<Employee> employees) { }

Normally the above method would be passed a collection of employees that were read from a database. However in your unit tests you don't want to access a database. So you create a fake employees list:

IList<Employee> fakeEmployees = new List<Employee>();

You can then add items to fakeEmployees and assert the expected results, in this case the total salary.

Mocks

When using mock objects you intend to verify some behaviour, or data, on those mock objects. Example:

You want to verify that a specific method was executed during a test run, here's a generic example using Moq mocking framework:

public void Test()
{
    // Arrange.
    var mock = new Mock<ISomething>();

    mock.Expect(m => m.MethodToCheckIfCalled()).Verifiable();

    var sut = new ThingToTest();

    // Act.
    sut.DoSomething(mock.Object);

    // Assert
    mock.Verify(m => m.MethodToCheckIfCalled());
}

Hopefully the above helps clarify things a bit.

EDIT: Roy Osherove is a well know advocate of Test Driven Development, and he has some very good information on the topic. You may find it very useful :

http://artofunittesting.com/