Stories
Slash Boxes
Comments
NOTE: use Perl; is on undef hiatus. You can read content, but you can't post it. More info will be forthcoming forthcomingly.

All the Perl that's Practical to Extract and Report

use Perl Log In

Log In

[ Create a new account ]

chromatic (983)

chromatic
  (email not shown publicly)
http://wgz.org/chromatic/

Blog Information [technorati.com] Profile for chr0matic [technorati.com]

Journal of chromatic (983)

Saturday March 13, 2004
12:45 AM

Mocking Parts of Objects

[ #17893 ]

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.

The Fine Print: The following comments are owned by whoever posted them. We are not responsible for them in any way.
 Full
 Abbreviated
 Hidden
More | Login | Reply
Loading... please wait.