|Syntactic Confectionery Delight|
Micro Mocking: using local to help test subsby adrianh (Chancellor)
|on Jan 13, 2003 at 01:19 UTC||Need Help??|
Writing this node reminded me of a testing technique I occasionally find useful.
Consider the following code:
I want to test that foo calls bar. I could check the text output by bar, but that ties my test to the what bar outputs. Since this can change independently I want to avoid this.
Solution: use local to redefine bar for the scope of the test. For example:
This technique allows you to mock only part of the class you are testing (hence "Micro Mocking" :-). Occasionally very useful.
You can, of course, use the same method for other classes too. For example, if you had:
You could check that Foo->new called CGI->new like this:
But, by this point, Test::MockObject is probably a more sensible choice :-)