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.