Mock objects documentation

This page...

What are mock objects?

Mock objects have two roles during a test case: actor and critic.

The actor behaviour is to simulate objects that are difficult to set up or time consuming to set up for a test. The classic example is a database connection. Setting up a test database at the start of each test would slow testing to a crawl and would require the installation of the database engine and test data on the test machine. If we can simulate the connection and return data of our choosing we not only win on the pragmatics of testing, but can also feed our code spurious data to see how it responds. We can simulate databases being down or other extremes without having to create a broken database for real. In other words, we get greater control of the test environment.

If mock objects only behaved as actors they would simply be known as "server stubs". This was originally a pattern named by Robert Binder (Testing object-oriented systems: models, patterns, and tools, Addison-Wesley) in 1999.

A server stub is a simulation of an object or component. It should exactly replace a component in a system for test or prototyping purposes, but remain lightweight. This allows tests to run more quickly, or if the simulated class has not been written, to run at all.

However, the mock objects not only play a part (by supplying chosen return values on demand) they are also sensitive to the messages sent to them (via expectations). By setting expected parameters for a method call they act as a guard that the calls upon them are made correctly. If expectations are not met they save us the effort of writing a failed test assertion by performing that duty on our behalf.

In the case of an imaginary database connection they can test that the query, say SQL, was correctly formed by the object that is using the connection. Set them up with fairly tight expectations and you will hardly need manual assertions at all.

Creating mock objects

All we need is an existing class or interface, say a database connection that looks like this...

class DatabaseConnection {
    function DatabaseConnection() { }
    function query($sql) { }
    function selectQuery($sql) { }
}
To create a mock version of the class we need to run a code generator...
require_once('simpletest/autorun.php');
require_once('database_connection.php');

Mock::generate('DatabaseConnection');
This code generates a clone class called MockDatabaseConnection. This new class appears to be the same, but actually has no behaviour at all.

The new class is usually a subclass of DatabaseConnection. Unfortunately, there is no way to create a mock version of a class with a final method without having a living version of that method. We consider that unsafe. If the target is an interface, or if final methods are present in a target class, then a whole new class is created, but one implemeting the same interfaces. If you try to pass this separate class through a type hint that specifies the old concrete class name, it will fail. Code like that insists on type hinting to a class with final methods probably cannot be safely tested with mocks.

If you want to see the generated code, then simply print the output of Mock::generate(). Here is the generated code for the DatabaseConnection class rather than the interface version...

class MockDatabaseConnection extends DatabaseConnection {
    public $mock;
    protected $mocked_methods = array('databaseconnection', 'query', 'selectquery');

    function MockDatabaseConnection() {
        $this->mock = new SimpleMock();
        $this->mock->disableExpectationNameChecks();
    }
    ...
    function DatabaseConnection() {
        $args = func_get_args();
        $result = &$this->mock->invoke("DatabaseConnection", $args);
        return $result;
    }
    function query($sql) {
        $args = func_get_args();
        $result = &$this->mock->invoke("query", $args);
        return $result;
    }
    function selectQuery($sql) {
        $args = func_get_args();
        $result = &$this->mock->invoke("selectQuery", $args);
        return $result;
    }
}
Your output may vary depending on the exact version of SimpleTest you are using.

Besides the original methods of the class, you will see some extra methods that help testing. More on these later.

We can now create instances of the new class within our test case...

require_once('simpletest/autorun.php');
require_once('database_connection.php');

Mock::generate('DatabaseConnection');

class MyTestCase extends UnitTestCase {

    function testSomething() {
        $connection = new MockDatabaseConnection();
    }
}
The mock version now has all the methods of the original. Also, any type hints will be faithfully preserved. Say our query methods expect a Query object...
class DatabaseConnection {
    function DatabaseConnection() { }
    function query(Query $query) { }
    function selectQuery(Query $query) { }
}
If we now pass the wrong type of object, or worse a non-object...
class MyTestCase extends UnitTestCase {

    function testSomething() {
        $connection = new MockDatabaseConnection();
        $connection->query('insert into accounts () values ()');
    }
}
...the code will throw a type violation at you just as the original class would.

The mock version now has all the methods of the original. Unfortunately, they all return null. As methods that always return null are not that useful, we need to be able to set them to something else...

Mocks as actors

Changing the return value of a method from null to something else is pretty easy...

$connection->returns('query', 37)
Now every time we call $connection->query() we get the result of 37. There is nothing special about 37. The return value can be arbitrarily complicated.

Parameters are irrelevant here, we always get the same values back each time once they have been set up this way. That may not sound like a convincing replica of a database connection, but for the half a dozen lines of a test method it is usually all you need.

Things aren't always that simple though. One common problem is iterators, where constantly returning the same value could cause an endless loop in the object being tested. For these we need to set up sequences of values. Let's say we have a simple iterator that looks like this...

class Iterator {
    function Iterator() { }
    function next() { }
}
This is about the simplest iterator you could have. Assuming that this iterator only returns text until it reaches the end, when it returns false, we can simulate it with...
Mock::generate('Iterator');

class IteratorTest extends UnitTestCase() {

    function testASequence() {
        $iterator = new MockIterator();
        $iterator->returns('next', false);
        $iterator->returnsAt(0, 'next', 'First string');
        $iterator->returnsAt(1, 'next', 'Second string');
        ...
    }
}
When next() is called on the MockIterator it will first return "First string", on the second call "Second string" will be returned and on any other call false will be returned. The sequenced return values take precedence over the constant return value. The constant one is a kind of default if you like.

Another tricky situation is an overloaded get() operation. An example of this is an information holder with name/value pairs. Say we have a configuration class like...

class Configuration {
    function Configuration() { ... }
    function get($key) { ... }
}
This is a likely situation for using mock objects, as actual configuration will vary from machine to machine and even from test to test. The problem though is that all the data comes through the get() method and yet we want different results for different keys. Luckily the mocks have a filter system...
$config = &new MockConfiguration();
$config->returns('get', 'primary', array('db_host'));
$config->returns('get', 'admin', array('db_user'));
$config->returns('get', 'secret', array('db_password'));
The extra parameter is a list of arguments to attempt to match. In this case we are trying to match only one argument which is the look up key. Now when the mock object has the get() method invoked like this...
$config->get('db_user')
...it will return "admin". It finds this by attempting to match the calling arguments to its list of returns one after another until a complete match is found.

You can set a default argument argument like so...


$config->returns('get', false, array('*'));
This is not the same as setting the return value without any argument requirements like this...

$config->returns('get', false);
In the first case it will accept any single argument, but exactly one is required. In the second case any number of arguments will do and it acts as a catchall after all other matches. Note that if we add further single parameter options after the wildcard in the first case, they will be ignored as the wildcard will match first. With complex parameter lists the ordering could be important or else desired matches could be masked by earlier wildcard ones. Declare the most specific matches first if you are not sure.

There are times when you want a specific reference to be dished out by the mock rather than a copy or object handle. This a rarity to say the least, but you might be simulating a container that can hold primitives such as strings. For example...

class Pad {
    function Pad() { }
    function &note($index) { }
}
In this case you can set a reference into the mock's return list...
function testTaskReads() {
    $note = 'Buy books';
    $pad = new MockPad();
    $vector->returnsByReference('note', $note, array(3));
    $task = new Task($pad);
    ...
}
With this arrangement you know that every time $pad->note(3) is called it will return the same $note each time, even if it get's modified.

These three factors, timing, parameters and whether to copy, can be combined orthogonally. For example...

$buy_books = 'Buy books';
$write_code = 'Write code';
$pad = new MockPad();
$vector->returnsByReferenceAt(0, 'note', $buy_books, array('*', 3));
$vector->returnsByReferenceAt(1, 'note', $write_code, array('*', 3));
This will return a reference to $buy_books and then to $write_code, but only if two parameters were set the second of which must be the integer 3. That should cover most situations.

A final tricky case is one object creating another, known as a factory pattern. Suppose that on a successful query to our imaginary database, a result set is returned as an iterator, with each call to the the iterator's next() giving one row until false. This sounds like a simulation nightmare, but in fact it can all be mocked using the mechanics above...

Mock::generate('DatabaseConnection');
Mock::generate('ResultIterator');

class DatabaseTest extends UnitTestCase {

    function testUserFinderReadsResultsFromDatabase() {
        $result = new MockResultIterator();
        $result->returns('next', false);
        $result->returnsAt(0, 'next', array(1, 'tom'));
        $result->returnsAt(1, 'next', array(3, 'dick'));
        $result->returnsAt(2, 'next', array(6, 'harry'));

        $connection = new MockDatabaseConnection();
        $connection->returns('selectQuery', $result);

        $finder = new UserFinder($connection);
        $this->assertIdentical(
                $finder->findNames(),
                array('tom', 'dick', 'harry'));
    }
}
Now only if our $connection is called with the query() method will the $result be returned that is itself exhausted after the third call to next(). This should be enough information for our UserFinder class, the class actually being tested here, to come up with goods. A very precise test and not a real database in sight.

We could refine this test further by insisting that the correct query is sent...

$connection->returns('selectQuery', $result, array('select name, id from people'));
This is actually a bad idea.

We have a UserFinder in object land, talking to database tables using a large interface - the whole of SQL. Imagine that we have written a lot of tests that now depend on the exact SQL string passed. These queries could change en masse for all sorts of reasons not related to the specific test. For example the quoting rules could change, a table name could change, a link table added or whatever. This would require the rewriting of every single test any time one of these refactoring is made, yet the intended behaviour has stayed the same. Tests are supposed to help refactoring, not hinder it. I'd certainly like to have a test suite that passes while I change table names.

As a rule it is best not to mock a fat interface.

By contrast, here is the round trip test...

class DatabaseTest extends UnitTestCase {
    function setUp() { ... }
    function tearDown() { ... }

    function testUserFinderReadsResultsFromDatabase() {
        $finder = new UserFinder(new DatabaseConnection());
        $finder->add('tom');
        $finder->add('dick');
        $finder->add('harry');
        $this->assertIdentical(
                $finder->findNames(),
                array('tom', 'dick', 'harry'));
    }
}
This test is immune to schema changes. It will only fail if you actually break the functionality, which is what you want a test to do.

The catch is those setUp() and tearDown() methods that we've rather glossed over. They have to clear out the database tables and ensure that the schema is defined correctly. That can be a chunk of extra work, but you usually have this code lying around anyway for deployment purposes.

One place where you definitely need a mock is simulating failures. Say the database goes down while UserFinder is doing it's work. Does UserFinder behave well...?

class DatabaseTest extends UnitTestCase {

    function testUserFinder() {
        $connection = new MockDatabaseConnection();
        $connection->throwOn('selectQuery', new TimedOut('Ouch!'));
        $alert = new MockAlerts();
        $alert->expectOnce('notify', 'Database is busy - please retry');
        $finder = new UserFinder($connection, $alert);
        $this->assertIdentical($finder->findNames(), array());
    }
}
We've passed the UserFinder an $alert object. This is going to do some kind of pretty notifications in the user interface in the event of an error. We'd rather not sprinkle our code with hard wired print statements if we can avoid it. Wrapping the means of output means we can use this code anywhere. It also makes testing easier.

To pass this test, the finder has to write a nice user friendly message to $alert, rather than propogating the exception. So far, so good.

How do we get the mock DatabaseConnection to throw an exception? We generate the exception using the throwOn method on the mock.

Finally, what if the method you want to simulate does not exist yet? If you set a return value on a method that is not there, SimpleTest will throw an error. What if you are using __call() to simulate dynamic methods?

Objects with dynamic interfaces, using __call, can be problematic with the current mock objects implementation. You can mock the __call() method, but this is ugly. Why should a test know anything about such low level implementation details? It just wants to simulate the call.

The way round this is to add extra methods to the mock when generating it.

Mock::generate('DatabaseConnection', 'MockDatabaseConnection', array('setOptions'));
In a large test suite this could cause trouble, as you probably already have a mock version of the class called MockDatabaseConnection without the extra methods. The code generator will not generate a mock version of the class if one of the same name already exists. You will confusingly fail to see your method if another definition was run first.

To cope with this, SimpleTest allows you to choose any name for the new class at the same time as you add the extra methods.

Mock::generate('DatabaseConnection', 'MockDatabaseConnectionWithOptions', array('setOptions'));
Here the mock will behave as if the setOptions() existed in the original class.

Mock objects can only be used within test cases, as upon expectations they send messages straight to the currently running test case. Creating them outside a test case will cause a run time error when an expectation is triggered and there is no running test case for the message to end up. We cover expectations next.

Mocks as critics

Although the server stubs approach insulates your tests from real world disruption, it is only half the benefit. You can have the class under test receiving the required messages, but is your new class sending correct ones? Testing this can get messy without a mock objects library.

By way of example, let's take a simple PageController class to handle a credit card payment form...

class PaymentForm extends PageController {
    function __construct($alert, $payment_gateway) { ... }
    function makePayment($request) { ... }
}
This class takes a PaymentGateway to actually talk to the bank. It also takes an Alert object to handle errors. This class talks to the page or template. It's responsible for painting the error message and highlighting any form fields that are incorrect.

It might look something like...

class Alert {
    function warn($warning, $id) {
        print '<div class="warning">' . $warning . '</div>';
        print '<style type="text/css">#' . $id . ' { background-color: red }</style>';
    }
}

Our interest is in the makePayment() method. If we fail to enter a "CVV2" number (the three digit number on the back of the credit card), we want to show an error rather than try to process the payment. In test form...

<?php
require_once('simpletest/autorun.php');
require_once('payment_form.php');
Mock::generate('Alert');
Mock::generate('PaymentGateway');

class PaymentFormFailuresShouldBeGraceful extends UnitTestCase {

    function testMissingCvv2CausesAlert() {
        $alert = new MockAlert();
        $alert->expectOnce(
                    'warn',
                    array('Missing three digit security code', 'cvv2'));
        $controller = new PaymentForm($alert, new MockPaymentGateway());
        $controller->makePayment($this->requestWithMissingCvv2());
    }

    function requestWithMissingCvv2() { ... }
}
?>
The first question you may be asking is, where are the assertions?

The call to expectOnce('warn', array(...)) instructs the mock to expect a call to warn() before the test ends. When it gets a call to warn(), it checks the arguments. If the arguments don't match, then a failure is generated. It also fails if the method is never called at all.

The test above not only asserts that warn was called, but that it received the string "Missing three digit security code" and also the tag "cvv2". The equivalent of assertIdentical() is applied to both fields when the parameters are compared.

If you are not interested in the actual message, and we are not for user interface code that changes often, we can skip that parameter with the "*" operator...

class PaymentFormFailuresShouldBeGraceful extends UnitTestCase {

    function testMissingCvv2CausesAlert() {
        $alert = new MockAlert();
        $alert->expectOnce('warn', array('*', 'cvv2'));
        $controller = new PaymentForm($alert, new MockPaymentGateway());
        $controller->makePayment($this->requestWithMissingCvv2());
    }

    function requestWithMissingCvv2() { ... }
}
We can weaken the test further by missing out the parameters array...
function testMissingCvv2CausesAlert() {
    $alert = new MockAlert();
    $alert->expectOnce('warn');
    $controller = new PaymentForm($alert, new MockPaymentGateway());
    $controller->makePayment($this->requestWithMissingCvv2());
}
This will only test that the method is called, which is a bit drastic in this case. Later on, we'll see how we can weaken the expectations more precisely.

Tests without assertions can be both compact and very expressive. Because we intercept the call on the way into an object, here of the Alert class, we avoid having to assert its state afterwards. This not only avoids the assertions in the tests, but also having to add extra test only accessors to the original code. If you catch yourself adding such accessors, called "state based testing", it's probably time to consider using mocks in the tests. This is called "behaviour based testing", and is normally superior.

Let's add another test. Let's make sure that we don't even attempt a payment without a CVV2...

class PaymentFormFailuresShouldBeGraceful extends UnitTestCase {

    function testMissingCvv2CausesAlert() { ... }

    function testNoPaymentAttemptedWithMissingCvv2() {
        $payment_gateway = new MockPaymentGateway();
        $payment_gateway->expectNever('pay');
        $controller = new PaymentForm(new MockAlert(), $payment_gateway);
        $controller->makePayment($this->requestWithMissingCvv2());
    }

    ...
}
Asserting a negative can be very hard in tests, but expectNever() makes it easy.

expectOnce() and expectNever() are sufficient for most tests, but occasionally you want to test multiple events. Normally for usability purposes we want all missing fields in the form to light up, not just the first one. This means that we should get multiple calls to Alert::warn(), not just one...

function testAllRequiredFieldsHighlightedOnEmptyRequest() {
    $alert = new MockAlert();
    $alert->expectAt(0, 'warn', array('*', 'cc_number'));
    $alert->expectAt(1, 'warn', array('*', 'expiry'));
    $alert->expectAt(2, 'warn', array('*', 'cvv2'));
    $alert->expectAt(3, 'warn', array('*', 'card_holder'));
    $alert->expectAt(4, 'warn', array('*', 'address'));
    $alert->expectAt(5, 'warn', array('*', 'postcode'));
    $alert->expectAt(6, 'warn', array('*', 'country'));
    $alert->expectCallCount('warn', 7);
    $controller = new PaymentForm($alert, new MockPaymentGateway());
    $controller->makePayment($this->requestWithMissingCvv2());
}
The counter in expectAt() is the number of times the method has been called already. Here we are asserting that every field will be highlighted.

Note that we are forced to assert the order too. SimpleTest does not yet have a way to avoid this, but this will be fixed in future versions.

Here is the full list of expectations you can set on a mock object in SimpleTest. As with the assertions, these methods take an optional failure message.
Expectation Description
expect($method, $args) Arguments must match if called
expectAt($timing, $method, $args) Arguments must match when called on the $timing'th time
expectCallCount($method, $count) The method must be called exactly this many times
expectMaximumCallCount($method, $count) Call this method no more than $count times
expectMinimumCallCount($method, $count) Must be called at least $count times
expectNever($method) Must never be called
expectOnce($method, $args) Must be called once and with the expected arguments if supplied
expectAtLeastOnce($method, $args) Must be called at least once, and always with any expected arguments
Where the parameters are...

$method
The method name, as a string, to apply the condition to.
$args
The arguments as a list. Wildcards can be included in the same manner as for setReturn(). This argument is optional for expectOnce() and expectAtLeastOnce().
$timing
The only point in time to test the condition. The first call starts at zero and the count is for each method independently.
$count
The number of calls expected.

If you have just one call in your test, make sure you're using expectOnce.
Using $mocked->expectAt(0, 'method', 'args); on its own will allow the method to never be called. Checking the arguments and the overall call count are currently independant. Add an expectCallCount() expectation when you are using expectAt() unless zero calls is allowed.

Like the assertions within test cases, all of the expectations can take a message override as an extra parameter. Also the original failure message can be embedded in the output as "%s".

References and related information...