A number of months ago I read Test Driven Development: By Example and became converted to the idea of TDD. Since that time, I have always tried to write the test first, using Marcus Baker's excellent Simple Test for PHP. It is a most satisfying way to write code, always knowing exactly when something works.

This past week I was working on a project (that I hope to publicly release real soon now ™), and I wanted to add a call from one object to another object that would end up sending an email to a user. I wanted to write the test first, but I didn't want to be sending an email for every test execution. I knew about mock objects, but I didn't know exactly how to approach getting the first object (the one under test) to use a mock without putting in some ugly test conditional in the class. Nor did I want to pass in a reference to the second object when testing the first one. What to do?

So, as I was considering this problem with my self-imposed restrictions, I was idly reading around on Marcus' site and found some guidance on using partial mocks. Aha! This was exactly what I wanted. By changing the way the first object creates the second object, I can then partially mock the object under test, replacing the method that returns the second object with one that returns the mock. Beautiful.

I know I've done a terrible job of explaining it, but it's a great testing tool. Just read that last link to clear up any confusion I caused you. :)