What I do is I have my controllers fetch all their dependencies from the
bootstrap and/or front controller. The most common example is to pull the db
resource from the bootstrap:

// in controller
$db = $this->getInvokeArg('bootstrap')->getResource('db');

But I also take it a step further. For example, if I'm using data mappers, I
have the action controller check the front controller for the data mapper I
need:

// in controller
$postsMapper = $this->getInvokeArg('posts_mapper');

I then update your unit test to inject the posts mapper with a stub:

// in unit test
$this->frontController->setParam('posts_mapper', $stubPostsMapper);

However, that invoke arg won't exist in production, so I wrap that call in
an if statement a la "lazy loading" style:

if (null === ($postsMapper = $this->getInvokeArg('posts_mapper'))) {
    $postsMapper = new Default_Model_Mapper_Posts();
}

What this does is it allows me to stub in my stub posts mapper in my unit
tests while letting the controller lazy-load the real one in production.

An alternative is to use Zend_Registry, but I find this to be a bit cleaner
without the static calls.

--
*Hector Virgen*
Sr. Web Developer
Walt Disney Parks and Resorts Online
http://www.virgentech.com



On Tue, Aug 10, 2010 at 12:08 PM, dmitrybelyakov <[email protected]>wrote:

>
> Hello,
>
> I rescently became a great fan of mocking everything when doing unit
> testing. Thanks to coolest 'mockery'
> by Pádraic Brady. Now I'm trying to make all my tests database independent.
>
> So the biggest problem here is mocking dependencies for controller tests. I
> see there was an attempt to do
> some sort of dependency injection container component, but unfortunately
> that proposal had some issues and
> was archived.
>
> I know I can go use one from symfony - just not sure if that's the right
> thing to do and it requires the 5.3.2
> php, that is not available on all our production environments.
>
> So I'm wondring - how are you guys doing that kind of thing?
>
> Dmitry.
> --
> View this message in context:
> http://zend-framework-community.634137.n4.nabble.com/Testing-controllers-in-isolation-tp2320293p2320293.html
> Sent from the Zend Framework mailing list archive at Nabble.com.
>

Reply via email to