Take the 2-minute tour ×

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?

share|improve this question
And while you're at it you might as well read this mockobjects.com/files/mockrolesnotobjects.pdf –  blank Feb 15 '13 at 10:21
I hope you'll pardon the commercial, but we wrote a whole book "Growing Object Oriented Software", that address the motivation for using mocks. –  Steve Freeman Apr 27 '13 at 10:52
add comment

1 Answer

up vote 7 down vote accepted

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


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);


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.


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.

    // 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 :


share|improve this answer
very informative. I always thought fakes and mocks were interchangeable. I learn new stuff every day here :) +1 –  bas Feb 15 '13 at 11:20
add comment

Your Answer


