Thanks for your comments...
On 25 Nov 2008, at 12:44, Matthew Weier O'Phinney wrote:
> -- 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
>
Brilliant, as ever. :-)
> 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.
I think it's always true that I need to push more logic into the model!
Here though I've been noticing the phenomenon just from testing the
branches in a CRUD controller: are the request params okay? , is it a
post request?, is the data valid?, does the model actually get
updated? , do we redirect to the right place? (and so on).
Once I add in some testing of the actual output (assertQuery etc...)
the testcases become quite long.
-- these later cases seem to me to be testing the view, rather than
the controller so I'll play with moving cases to tests/application/
views along similar lines as you suggest above.
Thanks again,
Carlton
没有评论:
发表评论