【问题标题】:testAction() function returns null on debug() in CakePhp testingtestAction() 函数在 CakePhp 测试中的 debug() 上返回 null
【发布时间】:2014-04-10 22:32:29
【问题描述】:

我正在尝试学习如何在 CakePhp 中使用单元测试,我正在尝试编写控制器测试。我阅读了有关 testAction() 和 debug() 函数的信息,但它对我不起作用,我的意思是,测试方法通过了,但 debug() 返回 null(因为 testAction 确实如此)

这是我的代码:

<?php
App::uses('Controller', 'Controller');
App::uses('View', 'View');
App::uses('PostsController', 'Controller');

class PostsControllerTest extends ControllerTestCase {
    public function setUp() {
       parent::setUp();
       $Controller = new Controller();
       $View = new View($Controller);
       $this->Posts = new PostsController($View);
    }

    public function testIndex() {
          $result = $this->testAction('Posts/Index');
        debug($result);        

    }
}

帖子/索引控制器返回存储在数据库中的所有帖子的列表。

【问题讨论】:

  • 试试$this-&gt;testAction('posts/index');

标签: php cakephp phpunit


【解决方案1】:

我假设您使用的是 CakePHP 2。

$this-&gt;testAction() 可以返回几个不同的结果,具体取决于您提供的选项。

例如,如果您将return 选项设置为varstestAction() 方法将返回已在测试操作中设置的变量数组:

public function testIndex() {
    $result = $this->testAction('/posts/index', array('return' => 'vars'));
    debug($result);
}

在此示例中,调试数据应该是您在 /posts/index 操作中设置的变量数组。

CakePHP 文档描述了您可以在此处返回的可能结果:http://book.cakephp.org/2.0/en/development/testing.html#choosing-the-return-type

请注意,默认选项 result 为您提供控制器操作返回的值。对于大多数控制器操作,这将是 null,因此您在示例中得到 null 是意料之中的。

【讨论】:

    【解决方案2】:

    mtnorthrop 的回答确实对我有用,但只有一次我也处理了我的网站的授权。 如果您的网站使用授权,那么 testAction('/action', array('return' => 'contents') 将返回 null。我已经看到了一些解决方案:

    一个是遵循这里给出的解决方案: CakePHP Unit Test Not Returning Content or View 您在 AppController::beforeFilter() 中检查您是否处于调试模式,如果是,请始终对用户进行身份验证:

    // For Mock Objects and Debug >= 2 allow all (this is for PHPUnit Tests)
    if(preg_match('/Mock_/',get_class($this)) && Configure::read('debug') >= 2){
        $this->Auth->allow();
    }
    

    另一个是遵循此讨论中给出的建议:https://groups.google.com/forum/#!topic/cake-php/eWCO2bf5t98 并使用 ControllerTestCase 的 generate 函数模拟 Auth 对象:

    class MyControllerTest extends ControllerTestCase {
        public function setUp() {
            parent::setUp();
            $this->controller = $this->generate('My',
                array('components' => array(
                    'Auth' => array('isAuthorized')
                ))
            );
            $this->controller->Auth->expects($this->any())
                ->method('isAuthorized')
                ->will($this->returnValue(true));
    
        }
    }
    

    注意(我使用的是 CakePhp 2.3.8)

    【讨论】:

    • 在生产代码中添加“我在测试中吗”检查绝不是一个好主意——模拟类是解决这个问题的好方法/正确方法。您应该提出并回答了一个新问题 - 虽然与此相关,但此答案不适用于 this 问题。
    • 理论上我同意@AD7six。但实践似乎不可避免地不同于理论。感谢@efeder 为我指明了正确的方向。我正在使用CakePHP's Security componentrequireSecure。我没有看到任何关于在 https 中使用 testAction 的文档,因此在调试模式下不使用 requireSecure 似乎是一个很好的解决方案。
    • @tyler 你应该考虑问一个关于这个的问题,因为它与这个问题/答案没有直接关系,并且用测试操​​作来模拟任何组件功能非常简单。
    猜你喜欢
    • 2014-04-26
    • 2017-08-06
    • 2015-10-10
    • 1970-01-01
    • 2015-09-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多