13 Mar 2004 chromatic   » (Master)

Mocking Parts of Objects:

The more I use Test::MockObject, the more I discover that I only need to mock small pieces of well-designed objects.

This came to a head today when I made the template() method of Jellybean::Response a little smarter. Instead of making repeated method calls to set individual pieces of template data, you can now pass a hash reference containing multiple pieces of data. That's less code! You can also request the whole template data in one chunk, too, but that's a separate issue.

My tests use a mock object to test that containers set template data correctly in the response object. After I improved template(), I converted one container to the new format. Suddenly, four tests failed. Since I'd mocked a template() method, the test code no longer matched the capabilities of the object it mocked.

Clearly, there was room in the world for Test::MockObject::Extends. So I wrote 52 lines of code (with liberal spacing and brace placement) and 44 lines of tests. More importantly, it required adding one line to the Jellybean test, changing another line, and deleting the not-so-accurate mocked template() implementation. Presto, all tests pass again.

I seem to promise to add documentation and post the code quite a bit lately, so replay the last few messages.

Oh yeah, dissatisfaction with the state of the world is great when it motivates me to improve things that need improving, but I originally set out today to add two small features to my article managing program. Of course, that lead me to make the templating simpler there, which lead to me refactoring and adding one nifty new feature to the Jellybean templating system.... So that's why I rarely finish anything. I'm too productive.

Latest blog entries     Older blog entries

New Advogato Features

New HTML Parser: The long-awaited libxml2 based HTML parser code is live. It needs further work but already handles most markup better than the original parser.

Keep up with the latest Advogato features by reading the Advogato status blog.

If you're a C programmer with some spare time, take a look at the mod_virgule project page and help us with one of the tasks on the ToDo list!