testAction()函数在CakePhp测试中的debug()上返回null

Joa*_*uez 5 php phpunit cakephp

我试图学习如何在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);        

    }
}
Run Code Online (Sandbox Code Playgroud)

帖子/索引控制器返回存储在DB中的所有帖子的列表.

mtn*_*rop 10

我假设你正在使用CakePHP 2.

$this->testAction() 可以返回一些不同的结果,具体取决于您提供的选项.

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

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

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

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

请注意,默认选项result为您提供控制器操作返回的值.对于大多数控制器操作而言,这将是null您所null期望的事实.