|
Mock-Objects... Oh, Oh! Oh!!! |
|
|
|
|
Written by Frotz
|
|
Wednesday, 02 May 2007 |
Slowly the dim bulb begins to burn a little brighter... I just got Mock-Objects this morning...
Problem Scenario:
I'm working on resolving my final code coverage boundaries for REST::Resource as part of my corrections for issues raised by the CPAN Testers and I found that I have a base that is three levels away from the portion of code that I'm trying to cover that is getting in my way. Now, we've all heard about mock objects this and mock objects that, but its not until the rubber hits the road that you finally get it. As was the case this morning. My specific problem is that in my test cases, FCGI will respond to one fake (test) request and then shunt all other requests around the loop that has lower than tolerated code coverage. I've been staring at this for weeks. Not really understanding FCGI or what is happening on the other side of the Perl / C language boundary (and not really trying either). As I was working through the problem of increasing code coverage, I noticed that sometimes it is useful to create classes that behave like the interface that I want it implement. So I did (but I didn't call it a Mock Object, or if I did, I called it a mock object). Then I started analyzing this problem.
I found that in order to make my 6 tests through this code run the way I wanted it to run, that I needed something stronger. These tests ran once in the direction that I wanted to run in, but the other 5 were being deflected from entering the loop... (<shaking a fist at the sky> "Darn you Red Baron!") What I really wanted was an instance of FCGI that would be have the way that I wanted it to behave. Modifying the original base class (which was not mine) was not an option. In fact, many CPAN Testers don't even put it into their Core Perl environments.
<A low thumping sound begins slowly in the background.>
What I needed was a way to make my tests flow around that one conditional under my control.
<The thumping gets louder and faster.>
I needed that one conditional...
<THUMP!>
If my test could instance a version of FCGI in the environment such that it behaved the way that I wanted <THUMP!> when I wanted <THUMP!> but only in my testing environment <THUMP!> I'd have a <THUMP!> "Mock Object" and be happy!
<silence>
Time for some ibuprofen... For some reason I have a dull headache this morning... ;-)
|