php - 在 CakePhp 测试中,testAction() 函数在 debug() 上返回 null

标签 php cakephp phpunit

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

这是我的代码:

<?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);        

    }
}

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

最佳答案

我假设您使用的是 CakePHP 2。

$this->testAction() 可以返回一些不同的结果,这取决于您给它的选项。

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

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 为您提供 Controller 操作返回 的值。对于大多数 Controller 操作,这将是 null,因此您在示例中获得 null 的事实是可以预料的。

关于php - 在 CakePhp 测试中,testAction() 函数在 debug() 上返回 null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12431127/

相关文章:

php - 无法将针对一个 id 的多个数据插入到 mySQL 表的多个字段中?

php - Cakephp SQLSTATE[42000] : Syntax error or access violation: 1064

php - Laravel 第二次测试失败

php - 将 json 数据插入 mysql 表文件 JSON 数据包含的键比我的 mysql 表中的键多

php - 数据直接存储到mysql数据库,无需使用php设置任何验证

javascript - Bootstrap : hide glyphicon-time from datetimepicker

PHPUnit - 测试上次插入 ID

unit-testing - 在 PHPUnit 中运行特定测试套件时,如何跳过运行缓慢的测试,但在需要完整代码覆盖率时仍然运行所有测试?

php - 生成 CSV 时更改值

javascript - 如何在 HTML/CSS/Javascript/PHP 上查找函数、类和 ID