Skip to content

RFC: Mocking protected methods

Update, 2011-06-16, 12:15 AM Thanks for the comments.

(I swear I had something like that before and it didn't work!) Here's the solution:

$userId = 61382;
$docId  = 'CLD2_62e029fc-1dae-4f20-873e-69facb64a21a';
$body   = '{"error":"missing","reason":"problem?"}';

$client = new Zend_Http_Client;
$client->setAdapter(new Zend_Http_Client_Adapter_Test);

$couchdb = $this->getMock(
    'CouchDB',
    array('makeRequest',),
    array($userId, $this->config,)
);
$couchdb->expects($this->once())
    ->method('makeRequest')
    ->will($this->returnValue(new Zend_Http_Response(200, array(), $body)));
$couchdb->setHttpClient($client);
$couchdb->getDocument($docId);

--- Original blog entry ---

I wrote a couple tests for a small CouchDB access wrapper today. But when I wrote the implementation itself, I realized that my class setup depends on an actual CouchDB server being available and here my journey began.

Example code

Consider the following example:

My objective is not to be able to test any of the protected methods directly, but to be able to supply a fixture so we don't have to setup CouchDB to run our testsuite. My fixture would replace makeRequest() and return a JSON string instead.

SF and the haters

So Sourceforge is complaining about people hating on Twitter. First off: haters gonna hate!. Sourceforge should know better and not feed the trolls.

Then, people complain for a reason. For years your website has been complicated and actual development (not distribution) has stagnated on Sourceforge. To me it seemed like the primary objective was maximizing income from advertisement. Yes, you guys relaunched a few times, but unfortunately never got it quite right. Having said that, of course this is about open source, but the platform is equally important. I don't want to suggest to copy Github, but they really manage to do a lot of things right.