【问题标题】:Zend_Test_PHPUnit_ControllerTestCase: Test view parameters and not rendered outputZend_Test_PHPUnit_ControllerTestCase:测试视图参数而不是渲染输出
【发布时间】:2010-03-23 09:19:03
【问题描述】:

我正在使用 Zend_Test_PHPUnit_ControllerTestCase 来测试我的控制器。这个类提供了各种测试渲染输出的方法,但我不想让我的视图脚本参与进来。我想测试我的视图的变量。有没有办法访问 controllers 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
        );

    }
}

【问题讨论】:

    标签: php unit-testing zend-framework phpunit


    【解决方案1】:

    如果您确实必须对视图进行断言,请使用Zend_Controller_Action_HelperBroker::getStaticHelper('viewRenderer')-&gt;view 获取它并对其进行断言。

    然而,Zend_Test 的意图是您将使用 xpath 查询或类似的东西来断言实际响应。这将允许您全面测试您的应用程序,而不仅仅是其中的一部分。

    如果你只是断言视图包含一个 var 并且它等于一个给定的东西,你并没有真正测试它是否以正确的方式使用。

    【讨论】:

      【解决方案2】:

      1) Zend_Test_PHPUnit_ControllerTestCase::_resetPlaceholders() 使用在Zend_Registry::getInstance() 中获得的singelton 并在其中搜索占位符。也许你可以模仿这种行为。

      2) 你试过$view = Zend_Layout::getMvcInstance()-&gt;getView() 了吗?我还没有测试过控制器,但是由于测试用例包括单例,也许这不会是一个很遥远的猜测。

      【讨论】:

      • 这些解决方案都不起作用。调度器在动作运行后取消设置控制器对象,所以恐怕我必须继承调度器。
      猜你喜欢
      • 1970-01-01
      • 2022-12-21
      • 1970-01-01
      • 2015-06-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-04-27
      • 1970-01-01
      相关资源
      最近更新 更多