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.
In the same way that we create server stubs, all we need is an
existing class, say a database connection that looks like this...
MockDatabaseConnection.
We can now create instances of the new class within
our test case...
The mock version of a class has all the methods of the original,
so that operations like
query()]]> are still
legal.
The return value will be null,
but we can change that with...
query()]]> we get
the result of 37.
We can set the return value to anything, say a hash of
imaginary database results or a list of persistent objects.
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.
We can also add extra methods to the mock when generating it
and choose our own class name...
setOptions()
existed in the original class.
This is handy if a class has used the PHP overload()
mechanism to add dynamic methods.
You can create a special mock to simulate this situation.
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...
next() is called on the
mock iterator 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...
getValue() method and yet
we want different results for different keys.
Luckily the mocks have a filter system...
getValue() method invoked
like this...
You can set a default argument argument like so...
There are times when you want a specific object to be
dished out by the mock rather than a copy.
The PHP4 copy semantics force us to use a different method
for this.
You might be simulating a container for example...
get(12)]]> is
called it will return the same
$thing each time.
This is compatible with PHP5 as well.
These three factors, timing, parameters and whether to copy,
can be combined orthogonally.
For example...
$stuff only on the third
call and only if two parameters were set the second of
which must be the integer 1.
That should cover most simple prototyping 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 next() giving
one row until false.
This sounds like a simulation nightmare, but in fact it can all
be mocked using the mechanics above.
Here's how...
$connection is called with the correct
query() 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.
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, suppose we have a
SessionPool class that we
want to add logging to.
Rather than grow the original class into something more
complicated, we want to add this behaviour with a decorator (GOF).
The SessionPool code currently looks
like this...
LoggingSessionPool.
In particular we would like to check that the
findSession() method is
called with the correct session ID in the cookie and that
it sent the message "Starting session $cookie"
to the logger.
Despite the fact that we are testing only a few lines of production code, here is what we would have to do in a conventional test case:
SessionPool object.findSession().
Instead, here is the complete test method using mock object magic...
findSession() is a factory
method the simulation of which is described above.
The point of departure comes with the first
expectOnce() call.
This line states that whenever
findSession() is invoked on the
mock, it will test the incoming arguments.
If it receives the single argument of a string "abc"
then a test pass is sent to the unit tester, otherwise a fail is
generated.
This was the part where we checked that the right session was asked for.
The argument list follows the same format as the one for setting
return values.
You can have wildcards and sequences and the order of
evaluation is the same.
We use the same pattern to set up the mock logger.
We tell it that it should have
message() invoked
once only with the argument "Starting session abc".
By testing the calling arguments, rather than the logger output,
we insulate the test from any display changes in the logger.
We start to run our tests when we create the new
LoggingSessionPool and feed
it our preset mock objects.
Everything is now under our control.
This is still quite a bit of test code, but the code is very
strict.
If it still seems rather daunting there is a lot less of it
than if we tried this without mocks and this particular test,
interactions rather than output, is always more work to set
up.
More often you will be testing more complex situations without
needing this level or precision.
Also some of this can be refactored into a test case
setUp() method.
Here is the full list of expectations you can set on a mock object in SimpleTest...
| Expectation | Needs tally() |
|---|---|
expect($method, $args) |
No |
expectAt($timing, $method, $args) |
No |
expectCallCount($method, $count) |
Yes |
expectMaximumCallCount($method, $count) |
No |
expectMinimumCallCount($method, $count) |
Yes |
expectNever($method) |
No |
expectOnce($method, $args) |
Yes |
expectAtLeastOnce($method, $args) |
Yes |
setReturn().
This argument is optional for expectOnce()
and expectAtLeastOnce().
expectMaximumCallCount()
is slightly different in that it will only ever generate a failure.
It is silent if the limit is never reached.
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".
There are three approaches to creating mocks including the one that SimpleTest employs. Coding them by hand using a base class, generating them to a file and dynamically generating them on the fly.
Mock objects generated with SimpleTest
are dynamic.
They are created at run time in memory, using
eval(), rather than written
out to a file.
This makes the mocks easy to create, a one liner,
especially compared with hand
crafting them in a parallel class hierarchy.
The problem is that the behaviour is usually set up in the tests
themselves.
If the original objects change the mock versions
that the tests rely on can get out of sync.
This can happen with the parallel hierarchy approach as well,
but is far more quickly detected.
The solution, of course, is to add some real integration tests. You don't need very many and the convenience gained from the mocks more than outweighs the small amount of extra testing. You cannot trust code that was only tested with mocks.
If you are still determined to build static libraries of mocks
because you want to simulate very specific behaviour, you can
achieve the same effect using the SimpleTest class generator.
In your library file, say mocks/connection.php for a
database connection, create a mock and inherit to override
special methods or add presets...
BasicMockConnection
rather than the usual MockConnection.
We then inherit from this to get our version of
MockConnection.
By intercepting in this way we can add behaviour, here setting
the default value of query() to be false.
By using the default name we make sure that the mock class
generator will not recreate a different one when invoked elsewhere in the
tests.
It never creates a class if it already exists.
As long as the above file is included first then all tests
that generated MockConnection should
now be using our one instead.
If we don't get the order right and the mock library
creates one first then the class creation will simply fail.
Use this trick if you find you have a lot of common mock behaviour or you are getting frequent integration problems at later stages of testing.