2008年10月1日星期三

Re: [fw-mvc] How to test action controller method that communicates with external resources.

-- Piotr Czachur <zimnyx@gmail.com> wrote
(on Wednesday, 01 October 2008, 04:07 PM +0200):
> My controller action method uses $this->_getWebServiceCommunication()
> to obtain object to communicate with remote web service.
> This object itself is well tested, but I dunno how can I test
> controller method that makes use of this object.

If the service is based on Zend_Service_Abstract, you may be able to
use the Mock Zend_Http_Client adapter for testing. All Z_S_A-derived
classes allow you to set the default HTTP client used statically. Just
do so prior to making your dispatch() call, and make sure you feed an
expected request/response pair, and you'll be good to go.

> I can mock controller action $this->_getWebServiceCommunication() to
> return fake object for testing purposes, but how can I make this
> controller object to be used during
> Zend_Test_PHPUnit_ControllerTestCase::dispatch() ?
>
> Sample code below:
>
> <?php
>
> class IndexController extends Zend_Controller_Action
> {
>
> // How can I test this method without connecting to external webservice
> public function indexAction()
> {
> // ...
> $this->_getWebServiceCommunication()->sendRequest(123);
> // ...
> }
>
> private function _getWebServiceCommunication()
> {
> return new Custom_WebServiceCommunication();
> }
>
>
> Cheers,
> Piotr Czachur
>

--
Matthew Weier O'Phinney
Software Architect | matthew@zend.com
Zend Framework | http://framework.zend.com/

没有评论: