php - Zend_Test_PHPUnit_ControllerTestCase : Test view parameters and not rendered output

标签 php unit-testing zend-framework phpunit

我正在使用 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/

相关文章:

php - 重写 Doctrine Trait 属性

java - 如何为未捕获的线程异常处理程序编写单元测试。

zend-framework - Zend Date-时差

php - 对于强大的、可扩展的开发平台,您会建议使用什么框架?

php - ZF2中如何配置Doctrine文件映射驱动

php - 最高分 - 从数据库中对数字进行排序

php - 使用 Swift 3 XCode 8 解析 JSON

c# - Silverlight 5 VS 2012 单元测试

ruby-on-rails - 如何测试 facebook 签名请求

php - php composer 如何知道 php 版本?