Getting started with Fake Objects – Part 1: why fake is better then mock

If you’ve been writing unit tests for a short while you might have noticed that writing good unit tests is hard. The object under test can be hard to create because it require dependencies that you just cannot provide or some complicated environment is needed just to make the test pass.

When writing a unit test one of the challenges is how to code around the dependencies of the object of his test. In other words – how to isolate the code under test from external dependencies.

Fake, Stubs and Mocks

There is a difference – at least for some people - between mocks, stubs and fakes. for the purpose of this post and every unit test you will ever write – it does not really matters.

Some mocking framework have the distinction between these types of objects and for the sake of terminology (which some people seem to like) I’ll explain the difference between the two:

In a nutshell you do not care what happens to the stub during the test, but you do want to know something about the interaction between objects when you’re using a mock.

For a short (and good) explanation of mocks take a look at Roy Osherove’s mock objects elevator speech.

I prefer to simply call them “fakes”, because unless the tool I’m using forces me to differentiate between them – I don’t really care what they are as long as I write a good unit test.

When to use Fake objects

If you need to simulate the behavior of a complex or hard to create object – use fake object.

There are other good reasons for faking an object:

Fake objects are used to isolate your code from external dependencies. You can write your own (called hand rolled mocks/fakes) but there is no reason to reinvent the wheel – there are isolation frameworks for most mainstream languages – check Wikipedia’s list of mock object frameworks for your language of choice.

What Isolation framework can do for you?

All isolation (Mocking) frameworks I came across do the following three:

  1. Create fake objects
  2. Set fake object’s behavior
  3. Verify method was/wasn’t called

Some frameworks have additional capabilities – depending on the programming language their internal operation (how they work).


In the following posts I will explain about each of the three above – along with code examples!

Labels: , ,