Zend_Test_PHPUnit_ControllerTestCase: Test view parameters and not rendered output
I'm using Zend_Test_PHPUnit_ControllerTestCase to test my controllers. This class provides various ways to test the rendered output, but I don't want to get my view scripts involved. I'd 开发者_C百科like to test my view's vars. Is there a way to access to the controllers view object?
Here is an example, what I'm trying to do:
<?php
class Controller extends Zend_Controller_Action
{
public function indexAction()
{
$this-view->foo = 'bar';
}
}
class ControllerTest extends Zend_Test_PHPUnit_ControllerTestCase
{
public function testShowCallsServiceFind()
{
$this->dispatch('/controller');
//doesn't work, there is no such method:
$this->assertViewVar('foo', 'bar');
//doesn't work, end_Test_PHPUnit_ControllerTestCase has no getView method:
$this->assertEquals(
'bar',
$this->getView()->foo
);
}
}
If you really must assert against the view, get it with Zend_Controller_Action_HelperBroker::getStaticHelper('viewRenderer')->view
and assert against it.
The intention of Zend_Test however is that you would assert against the actual response, using an xpath query or something similar. this will allow you to fully test your application, instead of just a part of it.
if you simply assert that the view contains a var and that it is equal to a given thing, you are not really testing that it has been used in the right way.
1) Zend_Test_PHPUnit_ControllerTestCase::_resetPlaceholders()
uses the singelton obtained in Zend_Registry::getInstance()
and searches it for placeholders. Maybe you could mimic that behaviour.
2) Did you try $view = Zend_Layout::getMvcInstance()->getView()
already? I haven't tested controllers yet but since the test cases includes singeltons, perhaps that wouldn't be such a far out guess.
精彩评论