我正在使用 Zend_Test_PHPUnit_ControllerTestCase 来测试我的 Controller 。此类提供了各种方法来测试渲染的输出,但我不想让我的 View 脚本参与其中。我想测试我的 View 的变量。有没有办法访问 Controller View 对象?
这是一个例子,我正在尝试做的事情:
<?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
);
}
}
最佳答案
如果您确实必须对 View 进行断言,请使用 Zend_Controller_Action_HelperBroker::getStaticHelper('viewRenderer')->view
获取它并对其进行断言。
然而,Zend_Test 的目的是您可以使用 xpath 查询或类似的东西对实际响应进行断言。这将使您能够全面测试您的应用程序,而不仅仅是其中的一部分。
如果您只是断言 View 包含一个 var 并且它等于给定的事物,那么您并没有真正测试它是否已以正确的方式使用。
关于php - Zend_Test_PHPUnit_ControllerTestCase : Test view parameters and not rendered output,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2498656/