2008年11月25日星期二

Re: [fw-mvc] How to break up controller test cases?

-- Carlton Gibson <carlton.gibson@noumenal.co.uk> wrote
(on Tuesday, 25 November 2008, 09:26 AM +0000):
> I'm wondering if you can help with a strategy for keeping test case
> classes to a reasonable size...
>
> Using Zend_Test, I am testing each branch of the conditional logic of
> each action as well as various facets of the actual output. As a result,
> I have controller test cases which are becoming _quite big_.
>
> What I'd like to do is to split each action out to a separate test file
> or else perhaps split the conditional logic tests into one file whilst
> the view testing stuff lives elsewhere.
>
> My question is how to achieve this given the PHPUnit naming conventions
> of IndexControllerTest testing IndexController and so on?

I think this is a great idea, actually. While it's technically named
"ControllerTestCase", it could very well simply be named "MvcTestCase",
as it simply provides a harness for testing the results of an MVC
dispatch. In other words, there's nothing tying you down to testing your
entire controller in one test case -- or not allowing you to test the
entire application in one test case, either!

If you decide to split by action, I'd do the following:

tests/
controllers/
IndexController/
FooTest.php -- IndexController_FooTest.php,
testing action "foo" of controller
"index"

This would give a good semantic layout for your test cases. I'd also
suggest that you put a @group annotation at the top of each individual
test case class indicating the controller being tested:

/**
* @group IndexController
*/
class IndexController_FooTest extends Zend_Test_PHPUnit_ControllerTestCase
{
}

which will allow you to then test a full controller at a time:

phpunit --group IndexController

One final word: If you have a lot of logic in your controllers, that's a
sign to me that you may need to push more logic into your models. Check
carefully to see which logic is application flow -- determining which
model to use, which view to use, headers to send, etc -- and which logic
is domain related, and make sure domain logic gets pushed into your
models.

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

没有评论: