Previous Up Next
Group tests Simple Test PHP Unit Test Framework Partial mocks

Mock objects

Table of Contents

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

  1. class DatabaseConnection {
  2.     function DatabaseConnection(}
  3.     function query($sql}
  4.     function selectQuery($sql}
  5. }
To create a mock version of the class we need to run a code generator...
  1. require_once('simpletest/autorun.php');
  2. require_once('database_connection.php');
  3.  
  4. 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...

  1. class MockDatabaseConnection extends DatabaseConnection {
  2.     public $mock;
  3.     protected $mocked_methods array('databaseconnection''query''selectquery');
  4.  
  5.     function MockDatabaseConnection({
  6.         $this->mock new SimpleMock();
  7.         $this->mock->disableExpectationNameChecks();
  8.     }
  9.     ...
  10.     function DatabaseConnection({
  11.         $args func_get_args();
  12.         $result &$this->mock->invoke("DatabaseConnection"$args);
  13.         return $result;
  14.     }
  15.     function query($sql{
  16.         $args func_get_args();
  17.         $result &$this->mock->invoke("query"$args);
  18.         return $result;
  19.     }
  20.     function selectQuery($sql{
  21.         $args func_get_args();
  22.         $result &$this->mock->invoke("selectQuery"$args);
  23.         return $result;
  24.     }
  25. }
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...

  1. require_once('simpletest/autorun.php');
  2. require_once('database_connection.php');
  3.  
  4. Mock::generate('DatabaseConnection');
  5.  
  6. class MyTestCase extends UnitTestCase {
  7.  
  8.     function testSomething({
  9.         $connection new MockDatabaseConnection();
  10.     }
  11. }
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...
  1. class DatabaseConnection {
  2.     function DatabaseConnection(}
  3.     function query(Query $query}
  4.     function selectQuery(Query $query}
  5. }
If we now pass the wrong type of object, or worse a non-object...
  1. class MyTestCase extends UnitTestCase {
  2.  
  3.     function testSomething({
  4.         $connection new MockDatabaseConnection();
  5.         $connection->query('insert into accounts () values ()');
  6.     }
  7. }
...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...

  1. $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...

  1. class Iterator {
  2.     function Iterator(}
  3.     function next(}
  4. }
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...
  1. Mock::generate('Iterator');
  2.  
  3. class IteratorTest extends UnitTestCase({
  4.  
  5.     function testASequence({
  6.         $iterator new MockIterator();
  7.         $iterator->returns('next'false);
  8.         $iterator->returnsAt(0'next''First string');
  9.         $iterator->returnsAt(1'next''Second string');
  10.         ...
  11.     }
  12. }
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...

  1. class Configuration {
  2.     function Configuration(... }
  3.     function get($key... }
  4. }
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...
  1. $config &new MockConfiguration();
  2. $config->returns('get''primary'array('db_host'));
  3. $config->returns('get''admin'array('db_user'));
  4. $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...
  1. $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...

  1. $config->returns('get'falsearray('*'));
This is not the same as setting the return value without any argument requirements like this...
  1. $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...

  1. class Pad {
  2.     function Pad(}
  3.     function &note($index}
  4. }
In this case you can set a reference into the mock's return list...
  1. function testTaskReads({
  2.     $note 'Buy books';
  3.     $pad new MockPad();
  4.     $vector->returnsByReference('note'$notearray(3));
  5.     $task new Task($pad);
  6.     ...
  7. }
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...

  1. $buy_books 'Buy books';
  2. $write_code 'Write code';
  3. $pad new MockPad();
  4. $vector-><strong>returnsByReferenceAt(0'note'$buy_booksarray('*'3));
  5. $vector-><strong>returnsByReferenceAt(1'note'$write_codearray('*'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...

  1. Mock::generate('DatabaseConnection');
  2. Mock::generate('ResultIterator');
  3.  
  4. class DatabaseTest extends UnitTestCase {
  5.  
  6.     function testUserFinderReadsResultsFromDatabase({
  7.         $result new MockResultIterator();
  8.         $result->returns('next'false);
  9.         $result->returnsAt(0'next'array(1'tom'));
  10.         $result->returnsAt(1'next'array(3'dick'));
  11.         $result->returnsAt(2'next'array(6'harry'));
  12.  
  13.         $connection new MockDatabaseConnection();
  14.         $connection->returns('selectQuery'$result);
  15.  
  16.         $finder new UserFinder($connection);
  17.         $this->assertIdentical(
  18.                 $finder->findNames(),
  19.                 array('tom''dick''harry'));
  20.     }
  21. }
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...

  1. $connection->returns('selectQuery'$resultarray('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...

  1. class DatabaseTest extends UnitTestCase {
  2.     function setUp(... }
  3.     function tearDown(... }
  4.  
  5.     function testUserFinderReadsResultsFromDatabase({
  6.         $finder new UserFinder(new DatabaseConnection());
  7.         $finder->add('tom');
  8.         $finder->add('dick');
  9.         $finder->add('harry');
  10.         $this->assertIdentical(
  11.                 $finder->findNames(),
  12.                 array('tom''dick''harry'));
  13.     }
  14. }
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...?

  1. class DatabaseTest extends UnitTestCase {
  2.  
  3.     function testUserFinder({
  4.         $connection new MockDatabaseConnection();
  5.         $connection->throwOn('selectQuery'new TimedOut('Ouch!'));
  6.         $alert new MockAlerts();
  7.         $alert->expectOnce('notify''Database is busy - please retry');
  8.         $finder new UserFinder($connection$alert);
  9.         $this->assertIdentical($finder->findNames()array());
  10.     }
  11. }
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.

  1. 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.

  1. 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...

  1. class PaymentForm extends PageController {
  2.     function __construct($alert$payment_gateway... }
  3.     function makePayment($request... }
  4. }
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...

  1. class Alert {
  2.     function warn($warning$id{
  3.         print '<div class="warning">' $warning '</div>';
  4.         print '<style type="text/css">#' $id ' { background-color: red }</style>';
  5.     }
  6. }

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...

  1. <?php
  2. require_once('simpletest/autorun.php');
  3. require_once('payment_form.php');
  4. Mock::generate('Alert');
  5. Mock::generate('PaymentGateway');
  6.  
  7. class PaymentFormFailuresShouldBeGraceful extends UnitTestCase {
  8.  
  9.     function testMissingCvv2CausesAlert({
  10.         $alert new MockAlert();
  11.         $alert->expectOnce(
  12.                     'warn',
  13.                     array('Missing three digit security code''cvv2'));
  14.         $controller new PaymentForm($alertnew MockPaymentGateway());
  15.         $controller->makePayment($this->requestWithMissingCvv2());
  16.     }
  17.  
  18.     function requestWithMissingCvv2(... }
  19. }
  20. ?>
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...

  1. class PaymentFormFailuresShouldBeGraceful extends UnitTestCase {
  2.  
  3.     function testMissingCvv2CausesAlert({
  4.         $alert new MockAlert();
  5.         $alert->expectOnce('warn'array('*''cvv2'));
  6.         $controller new PaymentForm($alertnew MockPaymentGateway());
  7.         $controller->makePayment($this->requestWithMissingCvv2());
  8.     }
  9.  
  10.     function requestWithMissingCvv2(... }
  11. }
We can weaken the test further by missing out the parameters array...
  1. function testMissingCvv2CausesAlert({
  2.     $alert new MockAlert();
  3.     $alert->expectOnce('warn');
  4.     $controller new PaymentForm($alertnew MockPaymentGateway());
  5.     $controller->makePayment($this->requestWithMissingCvv2());
  6. }
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...

  1. class PaymentFormFailuresShouldBeGraceful extends UnitTestCase {
  2.  
  3.     function testMissingCvv2CausesAlert(... }
  4.  
  5.     function testNoPaymentAttemptedWithMissingCvv2({
  6.         $payment_gateway new MockPaymentGateway();
  7.         $payment_gateway->expectNever('pay');
  8.         $controller new PaymentForm(new MockAlert()$payment_gateway);
  9.         $controller->makePayment($this->requestWithMissingCvv2());
  10.     }
  11.  
  12.     ...
  13. }
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...

  1. function testAllRequiredFieldsHighlightedOnEmptyRequest({
  2.     $alert new MockAlert();
  3.     $alert->expectAt(0'warn'array('*''cc_number'));
  4.     $alert->expectAt(1'warn'array('*''expiry'));
  5.     $alert->expectAt(2'warn'array('*''cvv2'));
  6.     $alert->expectAt(3'warn'array('*''card_holder'));
  7.     $alert->expectAt(4'warn'array('*''address'));
  8.     $alert->expectAt(5'warn'array('*''postcode'));
  9.     $alert->expectAt(6'warn'array('*''country'));
  10.     $alert->expectCallCount('warn'7);
  11.     $controller new PaymentForm($alertnew MockPaymentGateway());
  12.     $controller->makePayment($this->requestWithMissingCvv2());
  13. }
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".

Previous Up Next
Group tests Simple Test PHP Unit Test Framework Partial mocks

Documentation generated on Sun, 31 Oct 2010 16:30:44 -0500 by phpDocumentor 1.4.3