我正在尝试学习如何在 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/