You are currently browsing the Test Me posts tagged: mocks


PHP|Tek 2012 – Developer Testing 201: When to Mock and When to Integrate

I am honestly very excited to be going to Chicago this May to speak and teach at PHP|Tek ’12.

If you have read the description for Developer Testing 201: When to Mock and When to Integrate you might notice the last line, which says

This course is a continuation of Developer Testing 101.

As much as I have been told that I am terrifyingly adorable, this wasn’t me being cute or a joke. There is in fact a 101 level, but do not fear!

The summary for Developer Testing 101: Become a Testing Fanatic is

In this workshop we will cover the methodologies and three basic levels of testing, then we will deep dive into how to use PHPUnit to achieve developer testing. The tests may not be the prettiest, most robust, or efficient, but you should leave the course with the ability and confidence to write tests for your code.

Topics include: xUnit framework basics and workflows, test classification, asserts, data driven testing, and an introduction to mocking.

This is a beginner course, but seasoned veterans may discover features they never knew.

The short…

As long as you have experience with PHPUnit, you will be ready to consume the material in Developer Testing 201.

Bonus material to cover beforehand:

Can’t wait to see you there!

Mockery Test Case for PHPUnit

Mockery is a wonderful PHP mock object framework. It reminds me of Mockito for Java.

Mockery is far more fluent than MockObjects, the mock object framework packaged with PHPUnit. Mockery and MockObjects can work in the same environment, independent of one another.

Since Mockery is a free standing library, it can be used in PHPUnit, but you need to make sure you either have a loader that supports Mockery or use a bootstrap.php to use the loader packaged with Mockery. You will also need to consider using the TestListener packaged with Mockery to avoid having to remember to call Mockery::close() in tearDown for each TestCase, and a TestListener requires a PHPUnit XML configuration file.

All of this is do-able, but just not fun.

Now there is another alternative:

PHPUnit_Extensions_Mockery_TestCase

Using this new test case eliminates the bootstrap.php, TestListener with XML configuration, and explicit static calls to Mockery if you use Hamcrest matchers instead of Mockery matchers.

There is even a short hand for initializing mockery mocks, and support for counting Mockery expectations as assertions for better --strict mode support.

Check it out in the latest release of Etsy PHPUnit Extensions.

When to Write Your Own Mocks

Last post I mentioned that there is a time and a place when you could consider writing a mock by hand.

It is my humble opinion, that there is only one situation that warrants handwritten mocks: Prototyping.

What do I mean by prototyping?
When you write an application, or even add a feature to an existing application, it’s always comforting to be able to start the “whole stack” and perform exploratory testing, as a developer. I mean, you. The person writing the code, starting the “whole stack”, and using the application, or new feature, in your developer environment. This is your chance to try out whether or not the application, or feature, will come up and be usable enough. Any realistic resource on test driven development (TDD) will acknowledge that there is a prototyping phase, at the beginning of a new project.

Now why am I sticking “whole stack” in quotes?
… to annoy you. No, just kidding.

In your application stack, there is almost always some external dependency, service, that you did not write. Often times, at least one of those is either a pain or too expensive for you to have locally. Expensive could be pure cost in money, as in your company can’t afford or won’t pay for more licenses, or it could be that your developer environment just cannot handle the load of the dependency. The dependency could also be a third-party service that you cannot access from your developer machine because of firewall restrictions, or again, someone is too cheap to pay for the extra access. The short of it: It’s not always possible to test end-to-end in the development environment.

So how do handwritten mocks help with this?
Basically, there should be an implementation of the external dependency’s interface that is an in-memory instance. It can either provide fixed values, values from a config, or actually mock the behavior of the external dependency. Ideally, such an implementation would be provided by the implementor of the actual dependency since they should know best how the actual dependency should behave, but not all providers are so kind. If this is the case, then writing your mock is not necessarily a terrible idea even though it defies the rule: Only mock your own objects.

So now can I test my mock?
Last post I mentioned that testing your mocks could indicate that you are doing something wrong, or even many things wrong. If you write a mock for prototyping, remember to include it with your source code, and not your test code. Source code should have tests when possible, and test code should be so simple that it never needs test code. This way you can maintain this rule: Never include something defined in test code in an assert.

Help! My external dependency is being hostile
Hostile Dependency is possibly one of my favorite terms. In a later post I will describe how forcing hostile dependencies to adapt will make it easy to swap out dependencies later.


Tags